利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
生活随笔
收集整理的這篇文章主要介紹了
利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載來源?http://www.cnblogs.com/xiaoliangge/p/7642372.html
ASP.net Core自帶DI(依賴注入),用法如下:
services.AddScoped(typeof(IProductService), typeof(ProductService));如果服務(wù)較多,必定造成文件難以維護(hù)
所以需要利用反射批量實(shí)現(xiàn)注冊
核心代碼如下:
一個(gè)類可能間接繼承了多個(gè)接口(例如:public 和internal的接口),這里我們就以實(shí)現(xiàn)類為Key,所繼承的接口為value構(gòu)造一個(gè)集合
/// <summary> /// 獲取程序集中的實(shí)現(xiàn)類對應(yīng)的多個(gè)接口/// </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[]>();}注冊:
我們現(xiàn)在可以把具體的注冊例如
services.AddScoped(typeof(IProductService), typeof(ProductService));改為:
//集中注冊服務(wù)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支持//集中注冊服務(wù)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> /// 獲取程序集中的實(shí)現(xiàn)類對應(yīng)的多個(gè)接口/// </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[]>();}} }轉(zhuǎn)載于:https://www.cnblogs.com/pxtxws/p/8975173.html
總結(jié)
以上是生活随笔為你收集整理的利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五次会议记录:开始进一步需求分析,及初
- 下一篇: C#中的三种timer