废话少说,先上代码
 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},interface={v.Value[0]},{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

然后批量注入并测试,测试一切正常

//批量接口注入
            var accessImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.DataAccess", "Access");
            foreach (var v in accessImpleAndInterfaces)
                services.AddScoped(v.Value[0], v.Key);
            var logicImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.BusinessLogic", "Logic");
            foreach (var v in logicImpleAndInterfaces)

                services.AddScoped(v.Value[0], v.Key);

asp.netcore di 实现批量接口注入的更多相关文章

  1. ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...

  2. ASP.NET Core Web 应用程序系列(一)- 使用ASP.NET Core内置的IoC容器DI进行批量依赖注入(MVC当中应用)

    在正式进入主题之前我们来看下几个概念: 一.依赖倒置 依赖倒置是编程五大原则之一,即: 1.上层模块不应该依赖于下层模块,它们共同依赖于一个抽象. 2.抽象不能依赖于具体,具体依赖于抽象. 其中上层就 ...

  3. Asp.netCore 3.1控制器属性注入and异步事务Aop by AutoFac

    Aspect Oriented Programming(AOP)是较为热门的一个话题.AOP,国内我们都习惯称之为:面向切面编程 下面直接code 干货展示:(一般人我还不告诉,嘻嘻) 1:导入相关的 ...

  4. Server in ASP.NET-Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  5. 利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入

    ASP.net Core自带DI(依赖注入),用法如下: services.AddScoped(typeof(IProductService), typeof(ProductService)); 如果 ...

  6. ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享了在ASP.NET Core中如何使用Autofac替换自带DI进行构造函数的批量依赖注入,本章将和大家继续分享如何使之能够同时支持属性的批量依赖注入. 约定: 1.仓储层接口 ...

  7. ASP.NETCore使用AutoFac依赖注入

    原文:ASP.NETCore使用AutoFac依赖注入 实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using Sys ...

  8. ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下

    ADO.NET   一.ADO.NET概要 ADO.NET是.NET框架中的重要组件,主要用于完成C#应用程序访问数据库 二.ADO.NET的组成 ①System.Data  → DataTable, ...

  9. ASP.NET Core中的依赖注入(2):依赖注入(DI)

    IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用"好莱坞原则"是应用程序以被动的方式实现对流程的定制.我们可以采用若干设计 ...

随机推荐

  1. Spring Cloud Stream消息总线

    Springcloud 里面对于MQ的整合一个是前一篇的消息总线一个是本文介绍的消息驱动 大体要学习这么几个知识点: 课题:SpringCloud消息驱动Stream1.什么是SpringCloud消 ...

  2. dependencies devDependencies peerDependencies optionalDependencies区别

    原文链接: https://zhuanlan.zhihu.com/p/29855253 在一个Node.js项目中,package.json几乎是一个必须的文件,它的主要作用就是管理项目中所使用到的外 ...

  3. 80X86寄存器详解<转载>

    引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像  CPU ...

  4. 爬虫之MongoDB

    一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 1.易用性 MongoDB是一个面向文档(document-oriented)的数据库,而不是关系型数据库. 不采用关系型主要是为了 ...

  5. 不理解use explanatory variables

  6. kylin_学习_02_kylin使用教程

    一. 二.参考资料 1.kylin从入门到实战:实际案例

  7. python3.x学习笔记2018-02-05更新

    前言:python3.x部分学习笔记,有意交流学习者可加wechat:YWNlODAyMzU5MTEzMTQ=.如果笔记内容有错,请指出来. 对数据类型的操作 可变数据类型:列表,集合,字典 列表: ...

  8. angular复选框式js树形菜单(二)

    删除(过滤)树形结构某一个子节点: function filterTreeData(treeData){ angular.forEach(treeData,function(item){ if (it ...

  9. idea下建立bootstrap项目

    环境准备: 1.创建一个static web名为bootstrapDemo 2.在bootstrapDemo文件夹下安装bower npm install bower 会自动产生node-module ...

  10. 2018.7.24 Error Code

    来不及解释了,写下再说 -------------------------------------------- SUCCESS = 0, RTC_SELFTEST_FAILED = 1,      ...