当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/104921.html

相关文章:

  • 【Spring】Spring对IoC的实现
  • 正则表达式中的大括号-花括号{}有什么用?
  • Flutter 状态栏完美攻略
  • Packet Tracer - 连接有线和无线 LAN
  • 02 笔记本电脑m.2硬盘更换
  • 2.04 商品搜索功能实现
  • 【SEO基础】百度权重是什么意思及网站关键词应该怎么选?
  • Unity实现在3D模型标记
  • iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动
  • 性能调试【学习笔记】
  • 【taro react】---- 获取元素的位置和宽高等信息
  • Java【Spring】项目创建、存储和获取 Bean 的基本方式
  • docker minio安装
  • 设计模式-命令模式在Java中的使用示例-桌面程序自定义功能键
  • 分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列
  • Postgresql取消正在执行的任务或强制终止正在执行的任务
  • 【Linux】Centos7 的 Systemctl 与 创建系统服务 (shell脚本)
  • Redis集群Cluster搭建
  • swing组件应用
  • Spring学习记录----十五、面向切面编程AOP+十六、Spring对事务的支持
  • Color Correction (颜色校正)
  • Unity-缓存池
  • ubuntu samba 配置常见问题
  • vue3.3-TinyMCE:TinyMCE富文本编辑器基础使用
  • 基于以太坊+IPFS的去中心化数据交易方法及平台
  • NestJS 的 拦截器 学习
  • Spring AOP 中的代理对象是怎么创建出来的?
  • 解决@Scope(“prototype“)不生效的问题
  • Mybatis 知识点
  • PHP中关于is,between,in等运算符的用法是什么?