轉載來源 http://www.cnblogs.com/xiaoliangge/p/7642372.html ASP.net Core自帶DI(依賴註入),用法如下: 如果服務較多,必定造成文件難以維護 所以需要利用反射批量實現註冊 核心代碼如下: 一個類可能間接繼承了多個介面(例如:public ...
轉載來源 http://www.cnblogs.com/xiaoliangge/p/7642372.html
ASP.net Core自帶DI(依賴註入),用法如下:
services.AddScoped(typeof(IProductService), typeof(ProductService));
如果服務較多,必定造成文件難以維護
所以需要利用反射批量實現註冊
核心代碼如下:
一個類可能間接繼承了多個介面(例如:public 和internal的介面),這裡我們就以實現類為Key,所繼承的介面為value構造一個集合
/// <summary> /// 獲取程式集中的實現類對應的多個介面 /// </summary> /// <param name="assemblyName">程式集</param> public Dictionary<Type, Type[]> GetClassName(string assemblyName) { if (!String.IsNullOrEmpty(assemblyName)) { Assembly assembly = Assembly.Load(assemblyName); List<Type> ts = assembly.GetTypes().ToList(); var result = new Dictionary<Type, Type[]>(); foreach (var item in ts.Where(s => !s.IsInterface)) { var interfaceType = item.GetInterfaces(); result.Add(item, interfaceType); } return result; } return new Dictionary<Type, Type[]>(); }
註冊:
我們現在可以把具體的註冊例如
services.AddScoped(typeof(IProductService), typeof(ProductService));
改為:
//集中註冊服務 foreach (var item in GetClassName("Service")) { foreach (var typeArray in item.Value) { services.AddScoped(typeArray, item.Key); } }
完整代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; using Entity.Table; using DAL; using System.Reflection; using Service; namespace ASP.NetCoreAPI { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ProductContext>(options => options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//添加Mysql支持 //集中註冊服務 foreach (var item in GetClassName("Service")) { foreach (var typeArray in item.Value) { services.AddScoped(typeArray, item.Key); } } services.AddUnitOfWork<ProductContext>();//添加UnitOfWork支持 //services.AddScoped(typeof(IProductService), typeof(ProductService));//用ASP.NET Core自帶依賴註入(DI)註入使用的類 services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } /// <summary> /// 獲取程式集中的實現類對應的多個介面 /// </summary> /// <param name="assemblyName">程式集</param> public Dictionary<Type, Type[]> GetClassName(string assemblyName) { if (!String.IsNullOrEmpty(assemblyName)) { Assembly assembly = Assembly.Load(assemblyName); List<Type> ts = assembly.GetTypes().ToList(); var result = new Dictionary<Type, Type[]>(); foreach (var item in ts.Where(s => !s.IsInterface)) { var interfaceType = item.GetInterfaces(); result.Add(item, interfaceType); } return result; } return new Dictionary<Type, Type[]>(); } } }