利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
发布时间:2020-12-16 07:11:15 所属栏目:asp.Net 来源:网络整理
导读:原文: 利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入 ASP.net Core自带DI(依赖注入),用法如下: services.AddScoped( typeof (IProductService), typeof (ProductService)); 如果服务较多,必定造成文件难以维护 所以需要利用反射批量实现注册
原文:
利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
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[]>(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 需要在asp.net中以分钟为单位查找音频文件的长度
- 无法从readonly或禁用的文本框控件asp.net获取价
- entity-framework – DbContext未在ASP.Net MVC中
- 深入理解C# 3.x的新特性(2):Extension Method
- C#、Python中分别是怎么实现通过字符串获取实体类
- asp.net-mvc – 避免在业务层(MVC 3)中使用Selec
- ASP.NET MVC3模型验证DataAnnotations使用客户端
- 使用asp.net C#从代码隐藏中读取HTML元素
- asp.net-mvc – 如何在发布期间跳过文件夹上的删
- asp.net-mvc – 在ASP.NET MVC 3中授权当前用户对
热点阅读