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();
                }
            }
        }
    }
}
相关推荐
“抚琴”的人13 小时前
【机械视觉】C#+VisionPro联合编程———【六、visionPro连接工业相机设备】
c#·工业相机·visionpro·机械视觉
FAREWELL0007515 小时前
C#核心学习(七)面向对象--封装(6)C#中的拓展方法与运算符重载: 让代码更“聪明”的魔法
学习·c#·面向对象·运算符重载·oop·拓展方法
CodeCraft Studio15 小时前
Excel处理控件Spire.XLS系列教程:C# 合并、或取消合并 Excel 单元格
前端·c#·excel
勘察加熊人17 小时前
forms实现连连看
c#
hvinsion17 小时前
PPT助手:一款集计时、远程控制与多屏切换于一身的PPT辅助工具
c#·powerpoint·ppt·ppt助手·ppt翻页
weixin_3077791318 小时前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
时光追逐者19 小时前
在 Blazor 中使用 Chart.js 快速创建数据可视化图表
开发语言·javascript·信息可视化·c#·.net·blazor
与火星的孩子对话20 小时前
Unity3D开发AI桌面精灵/宠物系列 【三】 语音识别 ASR 技术、语音转文本多平台 - 支持科大讯飞、百度等 C# 开发
人工智能·unity·c#·游戏引擎·语音识别·宠物
response_L20 小时前
国产系统统信uos和麒麟v10在线打开word给表格赋值
java·c#·word·信创·在线编辑
MasterNeverDown20 小时前
Swagger2Md:让WebAPI文档生成变得轻松高效
c#