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

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

注入接口和实现。

使用方法:

//配置项调用ConfigContainer

public void ConfigureContainer(ContainerBuilder builder)
        {
            TestMicroService.ConfigContainer(builder);
        }

//service调用RegisteApiController

public void ConfigureServices(IServiceCollection services)
 {

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

}

单例需要:

.AsImplementedInterfaces().SingleInstance();

{
    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();
                }
            }
        }
    }
}

AssemblyUtils:

public static class AssemblyUtils
    {
        public static Assembly[] ListAssemblies(string searchPath, string searchPattern, SearchOption option = SearchOption.TopDirectoryOnly)
        {
            string[] fileName = FileUtils.GetFileName(searchPath, searchPattern, option);
            Assembly[] array = new Assembly[fileName.Length];
            for (int i = 0; i < array.Length; i++)
            {
                string assemblyFile = Path.Combine(searchPath, fileName[i]);
                array[i] = Assembly.LoadFrom(assemblyFile);
            }

            return array;
        }
    }

FileUtils:

public static class FileUtils
    {
        public static string[] GetFileName(string searchPath, string searchPattern, SearchOption option = SearchOption.TopDirectoryOnly)
        {
            string[] files = Directory.GetFiles(searchPath, searchPattern, option);
            if (files == null || files.Length < 1)
            {
                return null;
            }

            return files?.ToArray();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值