标签:bst ring reac text bre context sem foreach scope
传统的依赖注入确实简单,但是随着项目的扩展随之而来的问题又来了,因为传统的注入是单个类和接口注入的,加入项目的接口和类增加到了上百个的话,就需要在Startup.cs中复制注入上百次,虽然能解决问题,但是显然有点笨拙。
下面介绍一个程序集DI依赖注入,即通过反射进行文件注入
首先通过反射获取当前程序集
/// <summary> /// 通过程序集的名称加载程序集 /// </summary> /// <param name="assemblyName"></param> /// <returns></returns> public static Assembly GetAssemblyByName(string assemblyName) { return AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName)); }
程序集依赖注入方法,逻辑不多描述,全在注释中
/// <summary> /// 程序集依赖注入 /// </summary> /// <param name="services">服务实例</param> /// <param name="assemblyName">程序集名称。不带DLL</param> /// <param name="serviceLifetime">依赖注入的类型 可为空</param> public static void AddAssembly(this IServiceCollection services, string assemblyName, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped) { if (services == null) throw new ArgumentNullException(nameof(services) + "为空"); if (String.IsNullOrEmpty(assemblyName)) throw new ArgumentNullException(nameof(assemblyName) + "为空"); var assembly = RuntimeHelper.GetAssemblyByName(assemblyName); if (assembly == null) throw new DllNotFoundException(nameof(assembly) + ".dll不存在"); //找到当前程序集的类集合 var types = assembly.GetTypes(); //过滤筛选(是类文件,并且不是抽象类,不是泛型) var list = types.Where(o => o.IsClass && !o.IsAbstract && !o.IsGenericType).ToList(); if (list == null && !list.Any()) return; //遍历获取到的类 foreach (var type in list) { //然后获取到类对应的接口 var interfacesList = type.GetInterfaces(); //校验接口存在则继续 if (interfacesList == null || !interfacesList.Any()) continue; //获取到接口(第一个) var inter = interfacesList.First(); switch (serviceLifetime) { //根据条件,选择注册依赖的方法 case ServiceLifetime.Scoped: //将获取到的接口和类注册进去 services.AddScoped(inter, type); break; case ServiceLifetime.Singleton: services.AddScoped(inter, type); break; case ServiceLifetime.Transient: services.AddTransient(inter, type); break; } } }
具体在Startup.cs 中调用如下
//程序集依赖注入(General.Services程序集名称) services.AddAssembly("General.Services");
标签:bst ring reac text bre context sem foreach scope
原文地址:https://www.cnblogs.com/happygx/p/8859093.html