Skip to content

Commit

Permalink
解决ioc容器的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjunjun committed Dec 28, 2018
1 parent 065e922 commit 6b99c25
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 57 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ Demo 演示地址:http://gaobili.cn:8600/swagger/index.html
* T4代码生成器
* Quartz.Net 调度任务组件 https://github.com/quartznet/quartznet
* Quartz.Net 内置调度任务组件(待开发) https://github.com/quartznet/quartznet
* SignalR 内置消息提醒非轮询 (待开发)
数据库相关:
* AOP缓存 (待开发)

* Repository + DomainService 仓储模式编程 (借鉴同事的 - -!
* Repository + DomainService 仓储模式编程、IRepository + IDomainService解耦 (可以动态替换掉ORM 待开发
* Entity Framework Core 2.2 轻量级ORM框架

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Titan.AppService.ModelService;
using Microsoft.EntityFrameworkCore;
using Titan.Blog.AppService.ModelService;
using Titan.Model.DataModel;
using Titan.RepositoryCode;

namespace Titan.Blog.AppService.DomainService
{
public class AuthorDomainSvc
{
//private readonly IHostingEnvironment _hostingEnvironment;
//public AuthorDomainSvc(IHostingEnvironment hostingEnvironment)
//{
// _hostingEnvironment = hostingEnvironment;
//}
//private readonly AuthorSvc _authorSvc;
//public AuthorDomainSvc(AuthorSvc authorSvc)
//{
// _authorSvc = authorSvc;
//}
private readonly AuthorSvc _authorSvc;
private ModelRespositoryFactory<Author, Guid> _modelSvc;
public AuthorDomainSvc(AuthorSvc authorSvc, ModelRespositoryFactory<Author, Guid> modelSvc)
{
_authorSvc = authorSvc;
_modelSvc = modelSvc;
}

public List<Author> GetList()
{
//return _authorSvc.FindModelByValue(x => true);
return new List<Author>();
return _authorSvc.FindModelByValue(x => true);
//return new List<Author>();
}

public List<Author> SqlTest()
{
return _modelSvc.context.Set<Author>().FromSql("select * from Author").ToList();

}

public int CommandSql()
{
return _modelSvc.context.Database.ExecuteSqlCommand("INSERT INTO [TestDB].[dbo].[Author] ([Id], [AuthorName], [PKId]) VALUES ('1', '管理员', newid());");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Titan.Model.DataModel;
using Titan.RepositoryCode;

namespace Titan.AppService.ModelService
namespace Titan.Blog.AppService.ModelService
{
public class AuthorSvc
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class HttpContext
{
private static IHttpContextAccessor _accessor;

public static Microsoft.AspNetCore.Http.HttpContext Current => _accessor.HttpContext;
public static Microsoft.AspNetCore.Http.HttpContext Current => _accessor?.HttpContext;

internal static void Configure(IHttpContextAccessor accessor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Store(ModelBaseContext libraryDataContext)
/// <returns></returns>
private static string GetThreadName()
{
return Thread.CurrentThread.Name;
return Thread.CurrentThread.Name??"";
}
}
}
12 changes: 6 additions & 6 deletions Titan.Blog.WebAPP/Titan.Blog.WebAPP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.WebAPP", "Titan.Blog.WebAPP\Titan.Blog.WebAPP.csproj", "{F9A589A5-89F5-4760-BCAB-E7814EA15061}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.WebAPP", "Titan.Blog.WebAPP\Titan.Blog.WebAPP.csproj", "{F9A589A5-89F5-4760-BCAB-E7814EA15061}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.AppService", "Titan.Blog.AppService\Titan.Blog.AppService.csproj", "{DAA06C34-A0B9-4C0A-9590-2704D3568046}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.AppService", "Titan.Blog.AppService\Titan.Blog.AppService.csproj", "{DAA06C34-A0B9-4C0A-9590-2704D3568046}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.Model", "Titan.Blog.Model\Titan.Blog.Model.csproj", "{2246C325-1106-4419-BC77-6EE38B5AF9FB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.Model", "Titan.Blog.Model\Titan.Blog.Model.csproj", "{2246C325-1106-4419-BC77-6EE38B5AF9FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.Repository", "Titan.Blog.Repository\Titan.Blog.Repository.csproj", "{D232A43E-0720-4891-893C-B6BE6C25FE1B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.Repository", "Titan.Blog.Repository\Titan.Blog.Repository.csproj", "{D232A43E-0720-4891-893C-B6BE6C25FE1B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.Infrastructure", "Titan.Blog.Infrastructure\Titan.Blog.Infrastructure.csproj", "{00506196-9771-4ACC-AF22-FA8224A26547}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.Infrastructure", "Titan.Blog.Infrastructure\Titan.Blog.Infrastructure.csproj", "{00506196-9771-4ACC-AF22-FA8224A26547}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titan.Blog.CodeTool", "Titan.Blog.CodeTool\Titan.Blog.CodeTool.csproj", "{5D896C07-FF7A-47B7-A193-B63CF1C1139C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Titan.Blog.CodeTool", "Titan.Blog.CodeTool\Titan.Blog.CodeTool.csproj", "{5D896C07-FF7A-47B7-A193-B63CF1C1139C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Titan.Blog.WebAPP.Extensions;
using Titan.Blog.WebAPP.Swagger;
using Titan.Model.DataModel;
using Titan.AppService.ModelService;
using Titan.Blog.AppService.DomainService;

namespace Titan.Blog.WebAPP.Controllers
Expand All @@ -22,7 +21,6 @@ namespace Titan.Blog.WebAPP.Controllers
public class ImageTestController : ApiControllerBase
{
//构造函数注入上下文
//private readonly TitanContext _context;
private readonly AuthorDomainSvc _authorSvc;
public ImageTestController(AuthorDomainSvc authorSvc)
{
Expand Down Expand Up @@ -59,10 +57,12 @@ public HttpResponseMessage Get(string id)
public OpResult<List<Author>> EFCoreTest(string id)
{
//ModelBaseContext fds = new ModelBaseContext(new DbContextOptions<ModelBaseContext>());
//AuthorDomainSvc _authorSvc=new AuthorDomainSvc();
//var employee = _authorSvc.GetList();
//AuthorDomainSvc _authorSvc = new AuthorDomainSvc();
var employee = _authorSvc.GetList();
var data = _authorSvc.SqlTest();
var ok = _authorSvc.CommandSql();
//var employee = new List<Author>();
return new OpResult<List<Author>>(OpResultType.Success, "", new List<Author>());
return new OpResult<List<Author>>(OpResultType.Success, "", employee);

//using (var tran = _context.Database.BeginTransaction())
//{
Expand Down
84 changes: 56 additions & 28 deletions Titan.Blog.WebAPP/Titan.Blog.WebAPP/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Text.Unicode;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Titan.Blog.Infrastructure.AOP;
using Titan.Blog.Infrastructure.Auth.Policys;
using Titan.Blog.Infrastructure.HttpExtenions;
Expand All @@ -33,6 +34,8 @@
using Titan.Blog.WebAPP.Swagger;
using Titan.Model.DataModel;
using Titan.Blog.AppService.DomainService;
using Titan.Blog.AppService.ModelService;
using Titan.RepositoryCode;

namespace Titan.Blog.WebAPP
{
Expand Down Expand Up @@ -62,6 +65,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//services.AddScoped<AuthorDomainSvc>().AddScoped<AuthorSvc>().AddScoped<ModelRespositoryFactory<Author, Guid>>();
#region EF Core
//注入上下文对象
services.AddDbContext<ModelBaseContext>(options =>
Expand Down Expand Up @@ -270,47 +274,71 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
//注册要通过反射创建的组件
//builder.RegisterType<AdvertisementServices>().As<IAdvertisementServices>();
builder.RegisterType<BlogCacheAOP>();//可以直接替换其他拦截器
//builder.RegisterType<AuthorDomainSvc>();//可以直接替换其他拦截器
//var assemblysServices1 = Assembly.Load("Blog.Core.Services");
//builder.RegisterType<AuthorDomainSvc>();//可以直接替换其他拦截器
//var assemblysServices1 = Assembly.Load("Blog.Core.Services");
//将services填充到Autofac容器生成器中
builder.Populate(services);
//获取当前应用程序加载程序集(C/S应用中使用)
//var assembly = Assembly.GetExecutingAssembly();
//builder.RegisterAssemblyTypes(assembly); //注册所有程序集类定义的非静态类型


//builder.RegisterType<AuthorDomainSvc>();
//builder.RegisterType<AuthorSvc>();
//builder.RegisterType<ModelRespositoryFactory<Author, Guid>>();

// ※※★※※ 如果你是第一次下载项目,请先F6编译,然后再F5执行,※※★※※
// ※※★※※ 因为解耦,bin文件夹没有以下两个dll文件,会报错,所以先编译生成这两个dll ※※★※※


builder.RegisterType<ModelRespositoryFactory<Author, Guid>>();
var repositoryDllFile = Path.Combine(basePath, "Titan.Blog.Repository.dll");
//var assemblysRepository = Assembly.LoadFile(repositoryDllFile);//Assembly.Load("Titan.Blog.Repository");
var assemblysRepository = Assembly.Load("Titan.Blog.Repository");
builder.RegisterAssemblyTypes(assemblysRepository);

var servicesDllFile = Path.Combine(basePath, "Titan.Blog.AppService.dll");//获取项目绝对路径
var assemblysServices = Assembly.LoadFile(servicesDllFile);//直接采用加载文件的方法
builder.RegisterAssemblyTypes(assemblysServices).AsImplementedInterfaces();//指定已扫描程序集中的类型注册为提供所有其实现的接口。
builder.RegisterAssemblyTypes(assemblysServices)
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
.InterceptedBy(typeof(BlogCacheAOP));//允许将拦截器服务的列表分配给注册。可以直接替换其他拦截器

var infrastructureDllFile = Path.Combine(basePath, "Titan.Blog.Infrastructure.dll");//获取项目绝对路径
var assemblysInfrastructure = Assembly.LoadFile(infrastructureDllFile);//直接采用加载文件的方法
builder.RegisterAssemblyTypes(assemblysInfrastructure).AsImplementedInterfaces();//指定已扫描程序集中的类型注册为提供所有其实现的接口。

var modelDllFile = Path.Combine(basePath, "Titan.Blog.Model.dll");//获取项目绝对路径
var assemblysModel = Assembly.LoadFile(modelDllFile);//直接采用加载文件的方法
builder.RegisterAssemblyTypes(assemblysModel).AsImplementedInterfaces();//指定已扫描程序集中的类型注册为提供所有其实现的接口。
//var assemblysServices = Assembly.LoadFile(servicesDllFile);// Assembly.Load("Titan.Blog.AppService");//直接采用加载文件的方法
var assemblysServices = Assembly.Load("Titan.Blog.AppService");//直接采用加载文件的方法
builder.RegisterAssemblyTypes(assemblysServices);//指定已扫描程序集中的类型注册为提供所有其实现的接口。.InstancePerRequest()

var assemblysModel = Assembly.Load("Titan.Blog.Model");//直接采用加载文件的方法
builder.RegisterAssemblyTypes(assemblysModel);//指定已扫描程序集中的类型注册为提供所有其实现的接口。.InstancePerRequest()

////builder.RegisterAssemblyTypes(assemblysServices)
//// .AsImplementedInterfaces()
//// .InstancePerLifetimeScope()
//// .EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
//// .InterceptedBy(typeof(BlogCacheAOP));//允许将拦截器服务的列表分配给注册。可以直接替换其他拦截器

//var infrastructureDllFile = Path.Combine(basePath, "Titan.Blog.Infrastructure.dll");//获取项目绝对路径
//var assemblysInfrastructure = Assembly.LoadFile(infrastructureDllFile);//直接采用加载文件的方法
//builder.RegisterAssemblyTypes(assemblysInfrastructure).AsSelf();//指定已扫描程序集中的类型注册为提供所有其实现的接口。

//var modelDllFile = Path.Combine(basePath, "Titan.Blog.Model.dll");//获取项目绝对路径
//var assemblysModel = Assembly.LoadFile(modelDllFile);//直接采用加载文件的方法
//builder.RegisterAssemblyTypes(assemblysModel).AsSelf();//指定已扫描程序集中的类型注册为提供所有其实现的接口。



//var aa = Path.Combine(basePath, "Titan.Blog.WebAPP.dll");
//var bb = Assembly.LoadFile(aa);
//builder.RegisterAssemblyTypes(bb);

var repositoryDllFile = Path.Combine(basePath, "Titan.Blog.Repository.dll");
var assemblysRepository = Assembly.LoadFile(repositoryDllFile);
builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces();

var aa = Path.Combine(basePath, "Titan.Blog.WebAPP.dll");
var bb = Assembly.LoadFile(aa);
builder.RegisterAssemblyTypes(bb).AsImplementedInterfaces();

//将services填充到Autofac容器生成器中
builder.Populate(services);

//使用已进行的组件登记创建新容器
var applicationContainer = builder.Build();

//获取容器内的对象
//var data = applicationContainer.ComponentRegistry.Registrations
// .Where(x => x.Activator.LimitType.ToString().Contains("Titan.RepositoryCode")).ToList();
//var data1 = applicationContainer.ComponentRegistry.Registrations
// .Where(x => x.Activator.LimitType.ToString().Contains("Titan.Blog.AppService")).ToList();

//var clas1 = applicationContainer.Resolve<AuthorDomainSvc>();
//Console.WriteLine(clas1.GetList());
#endregion

return new AutofacServiceProvider(applicationContainer);//第三方IOC接管 core内置DI容器
}
#endregion
Expand Down

0 comments on commit 6b99c25

Please sign in to comment.