在 上一篇 中將項目的基本骨架搭起來能正常跑通,這一篇將講到,如何通過autofac將DbContext和model進行解耦,只用添加model,而不用在DbContext中添加DbSet。
在這里就不詳細講autofac是干什么用的了,簡單說下autofac。
1.autofac可替換net core自帶的DI IOC,用來擴展。
2.autofac可提供Aop,具體實現在博客園有很多示例。
3.autofac的幾個生命周期用法:InstancePerDependency 每次都創建一個對象 ,SingleInstance 每次都是同一個對象,InstancePerLifetimeScope 同一個生命周期生成的對象是同一個。
接下來,我們需要在啟動項目上通過nuget安裝兩個Package:Autofac
、Autofac.Extensions.DependencyInjection。
因為autofac是通過接口來進行注入的,因此我們需要創建對應的基層接口用來注入。在basic項目通過nuget安裝Autofac.Extensions.DependencyInjection、,
然后中添加 Dependency 文件夾來存放基層接口,添加IOC容器接口:IIocManager,代碼如下:

using System; using Autofac; using Autofac.Core; namespace DemoFrame_Basic.Dependency { /// <summary> /// IOC容器接口 /// </summary> public interface IIocManager { IContainer Container { get; } bool IsRegistered(Type serviceType, ILifetimeScope scope = null); object Resolve(Type type, ILifetimeScope scope = null); T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class; T Resolve<T>(params Parameter[] parameters) where T : class; T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null); object ResolveOptional(Type serviceType, ILifetimeScope scope = null); object ResolveUnregistered(Type type, ILifetimeScope scope = null); T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class; ILifetimeScope Scope(); bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance); } }
再添加一個數據庫基礎接口:IEntityBase

/// <summary> /// 數據庫基礎接口 /// </summary> public interface IEntityBase { }
IIocManager的實現類:IocManager

using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Loader; using Autofac; using Autofac.Core; using Autofac.Core.Lifetime; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyModel; namespace DemoFrame_Basic.Dependency { /// <summary> /// Container manager /// </summary> public class IocManager : IIocManager { private IContainer _container; public static IocManager Instance { get { return SingletonInstance; } } private static readonly IocManager SingletonInstance = new IocManager(); /// <summary> /// Ioc容器初始化 /// </summary> /// <param name="config"></param> /// <returns></returns> public IServiceProvider Initialize(IServiceCollection services) { //.InstancePerDependency() //每次都創建一個對象 //.SingleInstance() //每次都是同一個對象 //.InstancePerLifetimeScope() //同一個生命周期生成的對象是同一個 var builder = new ContainerBuilder(); builder.RegisterInstance(Instance).As<IIocManager>().SingleInstance(); //所有程序集 和程序集下類型 var deps = DependencyContext.Default; var libs = deps.CompileLibraries.Where(lib => !lib.Serviceable && lib.Type != "package");//排除所有的系統程序集、Nuget下載包 var listAllType = new List<Type>(); foreach (var lib in libs) { try { var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name)); listAllType.AddRange(assembly.GetTypes().Where(type => type != null)); } catch { } } //注冊IEntityBase實現類 var entityBaseType = typeof(IEntityBase); var arrEntityBaseType = listAllType.Where(t => entityBaseType.IsAssignableFrom(t) && t != entityBaseType).ToArray(); builder.RegisterTypes(arrEntityBaseType) .AsImplementedInterfaces() .SingleInstance() .PropertiesAutowired(); foreach (var type in arrEntityBaseType) { if (type.IsClass && !type.IsAbstract && !type.BaseType.IsInterface && type.BaseType != typeof(object)) { builder.RegisterType(type).As(type.BaseType) .SingleInstance() .PropertiesAutowired(); } } //注冊controller實現類 讓Controller能被找到 var controller = typeof(ControllerBase); var arrcontrollerType = listAllType.Where(t => controller.IsAssignableFrom(t) && t != controller).ToArray(); builder.RegisterTypes(arrcontrollerType) .AsImplementedInterfaces() .SingleInstance() .PropertiesAutowired(); foreach (var type in arrcontrollerType) { if (type.IsClass && !type.IsAbstract && !type.BaseType.IsInterface && type.BaseType != typeof(object)) { builder.RegisterType(type).AsSelf(); } } builder.Populate(services); _container = builder.Build(); return new AutofacServiceProvider(_container); } /// <summary> /// Gets a container /// </summary> public virtual IContainer Container { get { return _container; } } /// <summary> /// Resolve /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="key">key</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class { if (scope == null) { //no scope specified scope = Scope(); } if (string.IsNullOrEmpty(key)) { return scope.Resolve<T>(); } return scope.ResolveKeyed<T>(key); } /// <summary> /// Resolve /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="key">key</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual T Resolve<T>(params Parameter[] parameters) where T : class { var scope = Scope(); return scope.Resolve<T>(parameters); } /// <summary> /// Resolve /// </summary> /// <param name="type">Type</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual object Resolve(Type type, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.Resolve(type); } /// <summary> /// Resolve all /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="key">key</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved services</returns> public virtual T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } if (string.IsNullOrEmpty(key)) { return scope.Resolve<IEnumerable<T>>().ToArray(); } return scope.ResolveKeyed<IEnumerable<T>>(key).ToArray(); } /// <summary> /// Resolve unregistered service /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class { return ResolveUnregistered(typeof(T), scope) as T; } /// <summary> /// Resolve unregistered service /// </summary> /// <param name="type">Type</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual object ResolveUnregistered(Type type, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } var constructors = type.GetConstructors(); foreach (var constructor in constructors) { try { var parameters = constructor.GetParameters(); var parameterInstances = new List<object>(); foreach (var parameter in parameters) { var service = Resolve(parameter.ParameterType, scope); if (service == null) throw new Exception("Unknown dependency"); parameterInstances.Add(service); } return Activator.CreateInstance(type, parameterInstances.ToArray()); } catch (Exception) { } } throw new Exception("No constructor was found that had all the dependencies satisfied."); } /// <summary> /// Try to resolve srevice /// </summary> /// <param name="serviceType">Type</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <param name="instance">Resolved service</param> /// <returns>Value indicating whether service has been successfully resolved</returns> public virtual bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance) { if (scope == null) { //no scope specified scope = Scope(); } return scope.TryResolve(serviceType, out instance); } /// <summary> /// Check whether some service is registered (can be resolved) /// </summary> /// <param name="serviceType">Type</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Result</returns> public virtual bool IsRegistered(Type serviceType, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.IsRegistered(serviceType); } /// <summary> /// Resolve optional /// </summary> /// <param name="serviceType">Type</param> /// <param name="scope">Scope; pass null to automatically resolve the current scope</param> /// <returns>Resolved service</returns> public virtual object ResolveOptional(Type serviceType, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.ResolveOptional(serviceType); } /// <summary> /// Get current scope /// </summary> /// <returns>Scope</returns> public virtual ILifetimeScope Scope() { try { //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return Container.BeginLifetimeScope(); } catch (Exception) { //we can get an exception here if RequestLifetimeScope is already disposed //for example, requested in or after "Application_EndRequest" handler //but note that usually it should never happen //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); } } } }
在這里添加完以后,我們需要將自帶的DI容器給替換成現在使用的autofac,
在啟動項目的Startup文件中更改,最終代碼如下:

public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>()); services.AddDbContext<DemoDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection"))); return IocManager.Instance.Initialize(services); }
為了方便使用,在CoreMvc項目中添加DemoWeb的類來存放一些系統數據:

using DemoFrame_Basic.Dependency; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using System.Linq; namespace DemoFrame_CoreMvc { public class DemoWeb { private static IHttpContextAccessor _httpContextAccessor; /// <summary> /// Configure /// </summary> /// <param name="httpContextAccessor"></param> public static void Configure(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } /// <summary> /// 當前請求HttpContext /// </summary> public static HttpContext HttpContext { get => _httpContextAccessor.HttpContext; set => _httpContextAccessor.HttpContext = value; } /// <summary> /// IocManager /// </summary> public static IIocManager IocManager { get; set; } /// <summary> /// Environment /// </summary> public static IHostingEnvironment Environment { get; set; } /// <summary> /// Configuration /// </summary> public static IConfiguration Configuration { get; set; } /// <summary> /// MemoryCache /// </summary> public static IMemoryCache MemoryCache { get; set; } /// <summary> /// 獲取當前請求客戶端IP /// </summary> /// <returns></returns> public static string GetClientIp() { var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault()?.Split(',')[0].Trim(); if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Connection.RemoteIpAddress.ToString(); } return ip; } } }
Startup的完整代碼如下:

public class Startup { public Startup(IConfiguration configuration) { DemoWeb.Configuration = configuration; Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>()); services.AddDbContext<DemoDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection"))); return IocManager.Instance.Initialize(services); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { DemoWeb.IocManager = app.ApplicationServices.GetService<IIocManager>(); DemoWeb.Environment = env; try//注意這里在本地開發允許時會重置數據庫,並清空所有數據,如不需要請注釋 { if (env.IsDevelopment()) { using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>() .CreateScope()) { var dbContent = serviceScope.ServiceProvider.GetService<DemoDbContext>(); //CheckMigrations(dbContent); var database = serviceScope.ServiceProvider.GetService<DemoDbContext>().Database; database.EnsureDeleted(); database.EnsureCreated(); } } } catch (Exception ex) { //LogHelper.Logger.Error(ex, "Failed to migrate or seed database"); } DemoWeb.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>()); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());//允許跨域 app.UseHttpsRedirection(); app.UseMvc(); } }
在這么多的配置都完成了的情況下,我們該去實現mode與DbContext的解耦操作了。那么該如何做呢?
廢話不多說了,直接上代碼,在數據庫上下文DemoDbContext中將之前的DbSet刪掉,更改如下:
!!!先將之前的model都繼承 IEntityBase 接口。這樣在模型生成時才能生成到數據庫!!!

/// <summary> /// 數據庫上下文 /// </summary> public class DemoDbContext : DbContext { public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options) { } #region IOC得到所有實體 private readonly IEntityBase[] _entitys = DemoWeb.IocManager.ResolveAll<IEntityBase>(); #endregion protected override void OnModelCreating(ModelBuilder modelBuilder) { if (_entitys == null) { return; } foreach (var entity in _entitys) { modelBuilder.Model.AddEntityType(entity.GetType()); } } }
在使用數據庫上下文是,使用Set<T>方法設置需要使用的的類
在下一篇中將介紹如何使用基類controller來統一前后端交互數據,統一使用一個模型進行返回。
有需要源碼的可通過此 GitHub 鏈接拉取 覺得還可以的給個 start 哦,謝謝!