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();
                }
            }
        }
    }
}
相关推荐
Artistation Game几秒前
一、c#基础
游戏·unity·c#·游戏引擎
chen_22728 分钟前
kanzi3.6.10 窗口插件-查找绑定信息
c#·kanzi
Murphy20233 小时前
.net4.0 调用API(form-data)上传文件及传参
开发语言·c#·api·httpwebrequest·form-data·uploadfile·multipart/form-
我曾经是个程序员4 小时前
C#Directory类文件夹基本操作大全
服务器·开发语言·c#
鸿喵小仙女5 小时前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf
一个不正经的林Sir5 小时前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
码农君莫笑15 小时前
使用blazor开发信息管理系统的应用场景
数据库·信息可视化·c#·.net·visual studio
可喜~可乐17 小时前
C# WPF开发
microsoft·c#·wpf
666和7771 天前
C#的单元测试
开发语言·单元测试·c#
小码编匠1 天前
WPF 星空效果:创建逼真的宇宙背景
后端·c#·.net