废话少说,先上代码
 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. 扩展KMP(转)

    拓展kmp是对KMP算法的扩展,它解决如下问题: 定义母串S,和字串T,设S的长度为n,T的长度为m,求T与S的每一个后缀的最长公共前缀,也就是说,设extend数组,extend[i]表示T与S[i ...

  2. Ubuntu 安装mysql

    ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server   2. apt-get isntall mysql-clie ...

  3. 安装,配置webpack.

    1.下载node.js 2.在需要用到webpack的项目下打开命令窗口运行npm init生成package.js 3.安装webpack,使用npm install webpack --save- ...

  4. 判断A字符串是B字符串的反转

    先将其中一个字符串进行反转操作,然后两个字符串进行判断. 1.反转 /** * 字符串反转 * @param str * @return */ private static String conver ...

  5. ssh学习(1)

    雇员薪资管理系统(crud) ①先搞定spring ②引入spring包 ③编写applicationContext.xml文件(或者beans.xml),我们把该文件放在src目录下 ④测试一下sp ...

  6. Word 2010怎么自动添加文献引用

    1.将光标移至在需要添加引用的地方,比如我下图中在这段文字最后添加一个引用(为了方便说明)   2.(2010版本) 3.点击上面的“引用”,然后点击蓝圈里面的小图标,出现下面对话框,并设置成如图,点 ...

  7. HDU 2203 kmp

    http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  8. Oracle Index Clustering Factor(集群因子)

    一.本文说明: 今天在做测试的时候发现字段上有索引,但是执行计划就是不走索引,经过在网上查找才发现原来是索引的集群因子过高导致的.本文属于转载 二.官网说明 The index clustering ...

  9. C#中的线程(二)线程同步

    C#中的线程(二)线程同步   Keywords:C# 线程Source:http://www.albahari.com/threading/Author: Joe AlbahariTranslato ...

  10. c++primer 第三章编程练习答案

    3.7.1 #include<iostream> int main() { using namespace std; ; int height,inch,foot; cout <&l ...