asp.netcore di 实现批量接口注入
发布时间:2020-12-16 07:29:17 所属栏目:asp.Net 来源:网络整理
导读:废话少说,先上代码 ?public static DictionaryType,Type[] GetImpleAndInterfaces(string assemblyName,string suffix) ? ? ? ? { ? ? ? ? ? ? if (!String.IsNullOrEmpty(assemblyName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Assembly assembly = Assembly.Load
废话少说,先上代码
?public static Dictionary<Type,Type[]> GetImpleAndInterfaces(string assemblyName,string suffix)
? ? ? ? {
? ? ? ? ? ? if (!String.IsNullOrEmpty(assemblyName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Assembly assembly = Assembly.Load(assemblyName);
? ? ? ? ? ? ? ? List<Type> types = assembly
? ? ? ? ? ? ? ? ? ? .GetTypes()
? ? ? ? ? ? ? ? ? ? .Where(x => !x.IsInterface && x.Name.Contains(suffix) && !x.IsGenericType)
? ? ? ? ? ? ? ? ? ? .ToList();
?
? ? ? ? ? ? ? ? var result = new Dictionary<Type,Type[]>();
? ? ? ? ? ? ? ? foreach (var item in types)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var interfaceType = item.GetInterfaces();
? ? ? ? ? ? ? ? ? ? result.Add(item,interfaceType);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? }
? ? ? ? ? ? return new Dictionary<Type,Type[]>();
? ? ? ? }
实际就是载入指定的DLL(根据DLL名字载入),然后过滤出非泛型且符合后缀的实现类(一般仓储,业务都有固定的后缀,可以自己定义命名规则,例如XXXReository,? XXXService等),并且找到实现类所继承的接口(根据我设想,最好是一个接口一个实现,如果遇到一个接口多个实现的地方,尽量自己手工注入,以免引起不必要的误会),进行单元测试,示例如下
?[TestMethod]
? ? ? ? public void BatchDITestMethod()
? ? ? ? {
? ? ? ? ? ? var accessImpleAndInterfaces= AssemblyHelper.GetImpleAndInterfaces("DJMS.DataAccess","Access");
? ? ? ? ? ? foreach(var v in accessImpleAndInterfaces)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"class={v.Key},interface={v.Value[0]},{v.Value.Length}");
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine($"Access接口及实现个数为:{accessImpleAndInterfaces.Count}");
?
? ? ? ? ? ? var logicImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.BusinessLogic","Logic");
? ? ? ? ? ? foreach (var v in logicImpleAndInterfaces)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"class={v.Key},{v.Value.Length}");
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine($"Logic接口及实现个数为:{accessImpleAndInterfaces.Count}");
? ? ? ? }
单元测试结果,发现没有问题,都可以找准
class=DJMS.DataAccess.Repository.DepartmentAccess,interface=DJMS.DataAccess.Repository.IDepartmentAccess,1
class=DJMS.DataAccess.Repository.ElemeTokenAccess,interface=DJMS.DataAccess.Repository.IElemeTokenAccess,1
class=DJMS.DataAccess.Repository.MenuAccess,interface=DJMS.DataAccess.Repository.IMenuAccess,1
class=DJMS.DataAccess.Repository.NewsAccess,interface=DJMS.DataAccess.Repository.INewsAccess,1
class=DJMS.DataAccess.Repository.NewsTypeAccess,interface=DJMS.DataAccess.Repository.INewsTypeAccess,1
class=DJMS.DataAccess.Repository.RoleAccess,interface=DJMS.DataAccess.Repository.IRoleAccess,1
class=DJMS.DataAccess.Repository.RoleMenuAccess,interface=DJMS.DataAccess.Repository.IRoleMenuAccess,1
class=DJMS.DataAccess.Repository.SMSAccess,interface=DJMS.DataAccess.Repository.ISMSAccess,1
class=DJMS.DataAccess.Repository.UserAccess,interface=DJMS.DataAccess.Repository.IUserAccess,1
class=DJMS.DataAccess.Repository.UserRoleAccess,interface=DJMS.DataAccess.Repository.IUserRoleAccess,1
Access接口及实现个数为:10
class=DJMS.BusinessLogic.User.UserLogic,interface=DJMS.BusinessLogic.User.IUserLogic,1
class=DJMS.BusinessLogic.UserRole.UserRoleLogic,interface=DJMS.BusinessLogic.UserRole.IUserRoleLogic,1
class=DJMS.BusinessLogic.SMS.SMSLogic,interface=DJMS.BusinessLogic.SMS.ISMSLogic,1
class=DJMS.BusinessLogic.Role.RoleLogic,interface=DJMS.BusinessLogic.Role.IRoleLogic,1
class=DJMS.BusinessLogic.Photo.PhotoLogic,interface=DJMS.BusinessLogic.Photo.IPhotoLogic,1
class=DJMS.BusinessLogic.News.NewsLogic,interface=DJMS.BusinessLogic.News.INewsLogic,1
class=DJMS.BusinessLogic.NewsType.NewsTypeLogic,interface=DJMS.BusinessLogic.NewsType.INewsTypeLogic,1
class=DJMS.BusinessLogic.Menu.MenuLogic,interface=DJMS.BusinessLogic.Menu.IMenuLogic,1
class=DJMS.BusinessLogic.Eleme.ElemeLogic,interface=DJMS.BusinessLogic.Eleme.IElemeLogic,1
class=DJMS.BusinessLogic.Department.DepartmentLogic,interface=DJMS.BusinessLogic.Department.IDepartmentLogic,1
class=DJMS.BusinessLogic.Base.BaseLogic,interface=DJMS.BusinessLogic.Base.IBaseLogic,1
Logic接口及实现个数为:10
然后批量注入并测试,测试一切正常 ? ? ? ? ? ? //批量接口注入
? ? ? ? ? ? ? ? services.AddScoped(v.Value[0],v.Key);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – DropDownListFor Unobtrusive Validation
- asp.net – .NET异常页面非人类可读
- 如何使用ASP.net C#将SQL select存储到gridview?
- ASP.NET MVC 3.0部分内部和外部表单具有不显眼的数据验证属
- 在ASP.NET MVC3中实现的示例项目插件jquery文件上传插件
- 将变量从ASP.net传递给JavaScript
- asp.net-mvc-3 – EntityType’x’没有定义键.定义此Entity
- asp.net-mvc – 未找到MVC 6 404
- asp.net-mvc – 第一次只触发了Ajax.ActionLink
- asp.net – 如何在C#中获取特定文件夹的总大小?
推荐文章
站长推荐
- asp.net – 反伪造cookie令牌和表单字段令牌在MV
- ASP.NET网站中的预定作业,无需购买专用服务器
- .net – IAuthenticationFilter.OnAuthenticatio
- asp.net – 在SessionPageStatePersister中保持V
- asp.net – 程序或函数期望未提供的参数
- templates – CheckBoxList多个选择:如何建模绑
- asp.net – 为什么我要将UnitOfWork与Repository
- asp.net-mvc-3 – Razor webgrid ajax分页和排序
- asp.net-mvc – 在EF 6中设置命令超时
- asp.net-mvc – MVC @ Html.CheckboxFor在表单提
热点阅读