欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.netcore di 实现批量接口注入

程序员文章站 2023-02-26 17:30:20
废话少说,先上代码 public static Dictionary GetImpleAndInterfaces(string assemblyName,string suffix) { if (!String.IsNullOrEmpty(assemblyName)) { ......
废话少说,先上代码
 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);