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();
                }
            }
        }
    }
}
相关推荐
开开心心就好2 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
一名用户4 小时前
unity实现自定义粒子系统
c#·unity3d·游戏开发
钢铁男儿6 小时前
C# 类和继承(扩展方法)
java·servlet·c#
爱炸薯条的小朋友6 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Rose 使者8 小时前
全球IP归属地查询接口如何用C#进行调用?
c#·api·ip地址
~plus~10 小时前
Harmony核心:动态方法修补与.NET游戏Mod开发
开发语言·jvm·经验分享·后端·程序人生·c#
htj1010 小时前
C# 使用正则表达式
正则表达式·c#
~plus~10 小时前
WPF八大法则:告别模态窗口卡顿
开发语言·经验分享·后端·程序人生·c#
就是有点傻10 小时前
使用WPF的Microsoft.Xaml.Behaviors.Wpf中通用 UI 元素事件
c#
[email protected]11 小时前
ASP.NET Core SignalR - 部分客户端消息发送
后端·asp.net·.netcore