dotnet 依赖注入-批量注入Controller,service,Dao

此类为扩展注入类,使用autofuc 仅供参考

注入接口和实现。

使用方法:

//配置项调用ConfigContainer

cs 复制代码
public void ConfigureContainer(ContainerBuilder builder)
        {
            TestMicroService.ConfigContainer(builder);
        }

//service调用RegisteApiController

cs 复制代码
public void ConfigureServices(IServiceCollection services)
 {

   TestMicroService
                .RegisteApiController(services)
                .AddApiExplorer()
                .AddAuthorization(); 

}
cs 复制代码
{
    public static class TestMicroService
    {
        private static readonly string START_PATH = AppContext.BaseDirectory;

        private static readonly string API_CONTROLLER_PARTTERN = "Test.*.ApiController.dll";

        private static readonly string SERVICE_INTERFACE_PARTTERN = "Test.*.Service.dll";

        private static readonly string SERVICE_IMPLEMENT_PARTTERN = "Test.*.ServiceImpl.dll";

        private static readonly string DAO_INTERFACE_PARTTERN = "Test.*.Dao.dll";

        private static readonly string DAO_IMPLEMENT_PARTTERN = "Test.*.DaoImpl.dll";

        public static IMvcCoreBuilder RegisteApiController(IServiceCollection services)
        {
            Assembly[] apiControllerAssemblies = AssemblyUtils.ListAssemblies(START_PATH, API_CONTROLLER_PARTTERN);
            return services.AddMvcCore().ConfigureApplicationPartManager(delegate (ApplicationPartManager c)
            {
                Assembly[] array = apiControllerAssemblies;
                foreach (Assembly assembly in array)
                {
                    c.ApplicationParts.Add(new AssemblyPart(assembly));
                }

                ControllerFeature controllerFeature = new ControllerFeature();
                c.PopulateFeature(controllerFeature);
                services.AddSingleton(controllerFeature.Controllers.Select((TypeInfo t) => t.AsType()).ToArray());
            });
        }

        public static void ConfigContainer(this ContainerBuilder builder)
        {
            Assembly[] array = AssemblyUtils.ListAssemblies(START_PATH, SERVICE_INTERFACE_PARTTERN);
            Assembly[] array2 = AssemblyUtils.ListAssemblies(START_PATH, SERVICE_IMPLEMENT_PARTTERN);
            Assembly[] array3 = AssemblyUtils.ListAssemblies(START_PATH, DAO_INTERFACE_PARTTERN);
            Assembly[] array4 = AssemblyUtils.ListAssemblies(START_PATH, DAO_IMPLEMENT_PARTTERN);
            if (array.Length != array2.Length || array3.Length != array4.Length)
            {
                throw new Exception("init error.some service or dao assembly are missing...");
            }

            Assembly[] array5 = array2;
            foreach (Assembly assembly in array5)
            {
                string interfaceName = assembly.GetName().Name!.Replace("Impl", "");
                Assembly assembly2 = array.FirstOrDefault((Assembly c) => c.GetName().Name == interfaceName);
                if (assembly2 != null)
                {
                    (from t in builder.RegisterAssemblyTypes(assembly2, assembly)
                     where t.Name.EndsWith("ServiceImpl")
                     select t).AsImplementedInterfaces();
                }
            }

            array5 = array4;
            foreach (Assembly assembly3 in array5)
            {
                string daoInterfaceName = assembly3.GetName().Name!.Replace("Impl", "");
                Assembly assembly4 = array3.FirstOrDefault((Assembly c) => c.GetName().Name == daoInterfaceName);
                if (assembly4 != null)
                {
                    (from t in builder.RegisterAssemblyTypes(assembly4, assembly3)
                     where t.Name.EndsWith("DaoImpl")
                     select t).AsImplementedInterfaces();
                }
            }
        }
    }
}
相关推荐
IT技术分享社区3 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
△曉風殘月〆7 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風9 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_6569747412 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo12 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo15 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发15 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术15 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒15 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル17 小时前
C# EF 使用
c#