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

.NET8/.NETCore 依赖注入:自动注入项目中所有接口和自定义类

ASP.NETCore/WebApi 自动依赖注入接口和类-开发框架文库

.NET8/.NETCore 依赖接口注入:自动注入项目中所有接口和自定义类

目录

  • 自定义依赖接口
  • 扩展类:HostExtensions AddInjectionServices方法
  • GlobalAssemblies 全局静态类
  • 测试

自定义依赖接口

需要依赖注入的类必须实现以下接口。

C#

    /// <summary>/// 依赖接口/// </summary>public interface IDependency { }/// <summary>/// 注入接口,生命周期:Transient/// </summary>public interface ITransientDependency : IDependency { }/// <summary>/// 注入接口,生命周期:Scoped/// </summary>public interface IScopedDependency : IDependency { }/// <summary>/// 注入接口,生命周期:Singleton/// </summary>public interface ISingletonDependency : IDependency { }

扩展类:HostExtensions AddInjectionServices方法

C#

 public static class HostExtensions{/// <summary>/// 自动注入接口, 注入到服务容器IServiceCollection/// </summary>/// <param name="services"></param>/// <returns></returns>public static IServiceCollection AddInjectionServices(this IServiceCollection services){//服务生命周期映射Dictionary<Type, ServiceLifetime> map = new Dictionary<Type, ServiceLifetime>{{ typeof(ITransientDependency),ServiceLifetime.Transient },{ typeof(IScopedDependency),ServiceLifetime.Scoped },{ typeof(ISingletonDependency),ServiceLifetime.Singleton },{ typeof(IDependency),ServiceLifetime.Scoped },};//获取程序集所有实体模型Typevar listTypes = GlobalAssemblies.GetTypes();foreach (var type in listTypes){map.ToList().ForEach(aMap =>{//依赖注入接口var interfaceDependency = aMap.Key;if (interfaceDependency.IsAssignableFrom(type) && interfaceDependency != type && !type.IsAbstract && type.IsClass){//注入实现Console.WriteLine("注入实现:" + type.FullName + ", " + aMap.Value.ToString());                        services.Add(new ServiceDescriptor(type, type, aMap.Value));//获取当前类的所有接口var interfaces = listTypes.Where(x => x.IsInterface && x.IsAssignableFrom(type) && x != interfaceDependency).ToList();//有接口,注入接口if (interfaces.Count > 0){interfaces.ForEach(@inteface =>{Console.WriteLine("注入接口:" + type.FullName + "," + @inteface.FullName + ", " + aMap.Value.ToString());services.Add(new ServiceDescriptor(@inteface, type, aMap.Value));});}}});};return services;}}

GlobalAssemblies 全局静态类

加载程序集Assembly。

作用:

  • 用于初始化CSFramework.EF组件(注册实体模型)
  • 用于获取所有接口和类,依赖注入服务

C#

    public static class GlobalAssemblies{/// <summary>/// 加载程序集Assembly。/// 作用:1.用于初始化CSFramework.EF组件(注册实体模型)///      2.用于获取所有接口和类,依赖注入服务/// </summary>/// <param name="hostBuilder"></param>/// <returns></returns>public static void LoadAssemblies(){//加载以下程序集(包含所有实体模型、自定义服务的程序集)GlobalAssemblies.Assemblies = new List<System.Reflection.Assembly>{//如:CSFramework.LicenseServerCore.dllSystem.Reflection.Assembly.Load("CSFramework.LicenseServerCore"),System.Reflection.Assembly.Load("CSFramework.Models"),};}/// <summary>/// WebApi框架所有程序集/// </summary>public static List<System.Reflection.Assembly> Assemblies { get; set; }/// <summary>/// WebApi框架所有类型Types/// </summary>public static List<System.Type> GetTypes(){return Assemblies.SelectMany(m => m.GetExportedTypes()).ToList();}}

测试

.NET8/.NETCore 依赖接口注入:自动注入项目中所有接口和实体类

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

相关文章:

  • Flutter:city_pickers省市区三级联动
  • Kafka-Connect自带示例
  • Hbase应用案例 随机号码生成
  • 论文阅读——量子退火Experimental signature of programmable quantum annealing
  • (长期更新)《零基础入门 ArcGIS(ArcMap) 》实验二----网络分析(超超超详细!!!)
  • go语言 Pool实现资源池管理数据库连接资源或其他常用需要共享的资源
  • mysql一个事务最少几次IO操作
  • 运输层总结
  • 【嵌套查询】.NET开源 ORM 框架 SqlSugar 系列
  • React 前端框架1
  • 【真正离线安装】Adobe Flash Player 32.0.0.156 插件离线安装包下载(无需联网安装)
  • 数据采集时,不同地区的动态IP数据质量有什么差异?
  • 【Python爬虫五十个小案例】爬取猫眼电影Top100
  • 等保测评和 ISO27001 都是信息保护,区别是什么?
  • Linux系统编程之进程创建
  • JAVA-IO
  • 动态系统特征分析:特征向量、特征值、频率与阻尼比、参与因子计算方法
  • 乐鑫发布 esp-iot-solution v2.0 版本
  • 动态代理如何加强安全性
  • Flutter 之 InheritedWidget
  • AI 助力开发新篇章:云开发 Copilot 深度体验与技术解析
  • MyBatis-Plus介绍及基本使用
  • SpringBoot 整合 Avro 与 Kafka
  • 支持JT1078和GB28181的流媒体服务器-LKM启动配置文件参数说明
  • 什么是隐式类型转换?隐式类型转换可能带来哪些问题? 显式类型转换(如强制类型转换)有哪些风险?
  • 量化交易新利器:阿布量化(AbuQuant)——金融研究者的得力助手
  • UI设计从入门到进阶,全能实战课
  • Uniapp自动调整元素高度
  • 软考高项经验分享:我的备考之路与实战心得
  • 安全关系型数据库查询新选择:Rust 语言的 rust-query 库深度解析