ASP.net Core自帶DI(依賴注入),用法以下:sql
services.AddScoped(typeof(IProductService), typeof(ProductService));
若是服務較多,一定形成文件難以維護app
因此須要利用反射批量實現註冊ui
一個類可能間接繼承了多個接口(例如:public 和internal的接口),這裏咱們就以實現類爲Key,所繼承的接口爲value構造一個集合this
/// <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[]>(); }
咱們如今能夠把具體的註冊例如spa
services.AddScoped(typeof(IProductService), typeof(ProductService));
改成:.net
//集中註冊服務 foreach (var item in GetClassName("Service")) { foreach (var typeArray in item.Value) { services.AddScoped(typeArray, item.Key); } }
完整代碼:code
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[]>(); } } }