废话少说,先上代码
 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. java创建多线程的三种方式

    /***************************继承Thread类创建多线程************************/ public class FirstThread extends ...

  2. OpenStack for NFV applications: enabling Single Root I/O virtualization and PCI-Passthrough

    http://superuser.openstack.org/articles/openstack-for-nfv-applications-enabling-single-root-i-o-virt ...

  3. Linux文件压缩/打包/解压

    在Linux日常维护中,经常需要备份同步一些比较重要的文件,而在传输过程中如果文件比较大往往会非常慢,而且还会非常占用空间,这时候就需要我们使用压缩工具对大文件进行压缩打包,下面我们来介绍一下常用的压 ...

  4. jsp:jstl标签库国际化 fmt

    首先资源文件目录是:src/man/sources (这是它默认的资源文件) i18n_en_US.properties文件内容 copyright=Copyright \u00A9 2013 lan ...

  5. mysql: instr 多个字段 like数据

    你是否一直在寻找比MySQL的LIKE语句更高效的方法的,下面我就为你介绍几种. SELECT * FROM `order_shop` where instr(uuid,  'b') > 0 g ...

  6. php开发工程师面必问题

    随着培训机构的增加,越来越多的php从业者流入市场,从而影响了php就业环境.公司对人才的要求越来越高,而技术者本身也要技术过硬,学习越来越多的东西,因为只有这样,你才能跑在别人前面,才不被市场抛弃, ...

  7. 三目运算符与if else的运行效率

    两者的效率比较: 当比较次数较少时,效率一样: 当比较次数较多时,发现ifelse的速度更快,应该是其汇编指令更少的原因.   if else的汇编代码如下:     三目运算符代码如下:

  8. 剑指offer--9.字符串的排列

    next_permutation(),还是那个feel ------------------------------------------------------------------------ ...

  9. Leetcode 509. Fibonacci Number

    class Solution(object): def fib(self, N): """ :type N: int :rtype: int ""&q ...

  10. hdu4451 Dressing(容斥原理)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...