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

.NET 依赖注入(DI)全面解析

在这里插入图片描述

文章目录

    • 一、依赖注入核心原理
      • 1. 控制反转(IoC)与DI关系
      • 2. .NET DI核心组件
    • 二、服务生命周期
      • 1. 三种生命周期类型
    • 三、DI容器实现原理
      • 1. 服务注册流程
      • 2. 服务解析流程
    • 四、高级实现方法
      • 1. 工厂模式注册
      • 2. 泛型服务注册
      • 3. 多实现解决方案
    • 五、ASP.NET Core中的DI集成
      • 1. 控制器注入
      • 2. 视图注入
      • 3. 中间件注入
    • 六、自定义DI容器实现
      • 1. 简易DI容器实现
      • 2. 属性注入实现
    • 七、最佳实践
      • 1. 服务设计原则
      • 2. 常见陷阱
    • 八、性能优化
      • 1. 避免过度注入
      • 2. 编译时注入

在这里插入图片描述

一、依赖注入核心原理

1. 控制反转(IoC)与DI关系

  • 控制反转(IoC):框架控制程序流程,而非开发者
  • 依赖注入(DI):IoC的一种实现方式,通过外部提供依赖对象

2. .NET DI核心组件

  • IServiceCollection:服务注册容器
  • IServiceProvider:服务解析器
  • ServiceDescriptor:服务描述符(包含生命周期信息)

二、服务生命周期

1. 三种生命周期类型

生命周期描述适用场景
Transient每次请求创建新实例轻量级、无状态服务
Scoped同一作用域内共享实例Web请求上下文
Singleton全局单例配置服务、缓存
// 注册示例
services.AddTransient<ITransientService, TransientService>();
services.AddScoped<IScopedService, ScopedService>();
services.AddSingleton<ISingletonService, SingletonService>();

三、DI容器实现原理

1. 服务注册流程

public static IServiceCollection AddTransient<TService, TImplementation>(this IServiceCollection services)
{// 创建服务描述符var descriptor = new ServiceDescriptor(typeof(TService),typeof(TImplementation),ServiceLifetime.Transient);// 添加到集合services.Add(descriptor);return services;
}

2. 服务解析流程

public object GetService(Type serviceType)
{// 1. 查找服务描述符var descriptor = _descriptors.FirstOrDefault(d => d.ServiceType == serviceType);// 2. 根据生命周期创建实例if (descriptor.Lifetime == ServiceLifetime.Singleton){if (_singletons.TryGetValue(serviceType, out var instance))return instance;instance = CreateInstance(descriptor);_singletons[serviceType] = instance;return instance;}// ...处理Scoped和Transient
}

四、高级实现方法

1. 工厂模式注册

services.AddTransient<IService>(provider => {var otherService = provider.GetRequiredService<IOtherService>();return new ServiceImpl(otherService, "参数");
});

2. 泛型服务注册

services.AddTransient(typeof(IRepository<>), typeof(Repository<>));

3. 多实现解决方案

// 注册多个实现
services.AddTransient<IMessageService, EmailService>();
services.AddTransient<IMessageService, SmsService>();// 解析时获取所有实现
var services = provider.GetServices<IMessageService>();

五、ASP.NET Core中的DI集成

1. 控制器注入

public class HomeController : Controller
{private readonly ILogger _logger;public HomeController(ILogger<HomeController> logger){_logger = logger; // 自动注入}
}

2. 视图注入

@inject IConfiguration Config
<p>当前环境: @Config["Environment"]</p>

3. 中间件注入

public class CustomMiddleware
{private readonly RequestDelegate _next;private readonly ILogger _logger;public CustomMiddleware(RequestDelegate next,ILogger<CustomMiddleware> logger){_next = next;_logger = logger;}public async Task InvokeAsync(HttpContext context){// 使用注入的服务_logger.LogInformation("中间件执行");await _next(context);}
}

六、自定义DI容器实现

1. 简易DI容器实现

public class SimpleContainer : IServiceProvider
{private readonly Dictionary<Type, ServiceDescriptor> _descriptors;public SimpleContainer(IEnumerable<ServiceDescriptor> descriptors){_descriptors = descriptors.ToDictionary(x => x.ServiceType);}public object GetService(Type serviceType){if (!_descriptors.TryGetValue(serviceType, out var descriptor))return null;if (descriptor.ImplementationInstance != null)return descriptor.ImplementationInstance;var type = descriptor.ImplementationType ?? descriptor.ServiceType;return ActivatorUtilities.CreateInstance(this, type);}
}

2. 属性注入实现

public static class PropertyInjectionExtensions
{public static void AddPropertyInjection(this IServiceCollection services){services.AddTransient<IStartupFilter, PropertyInjectionStartupFilter>();}
}public class PropertyInjectionStartupFilter : IStartupFilter
{public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next){return builder =>{builder.Use(async (context, nextMiddleware) =>{var endpoint = context.GetEndpoint();if (endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>() is { } descriptor){var controller = context.RequestServices.GetRequiredService(descriptor.ControllerTypeInfo);// 反射实现属性注入InjectProperties(controller, context.RequestServices);}await nextMiddleware();});next(builder);};}private void InjectProperties(object target, IServiceProvider services){var properties = target.GetType().GetProperties().Where(p => p.CanWrite && p.GetCustomAttribute<InjectAttribute>() != null);foreach (var prop in properties){var service = services.GetService(prop.PropertyType);if (service != null)prop.SetValue(target, service);}}
}

七、最佳实践

1. 服务设计原则

  • 遵循显式依赖原则
  • 避免服务定位器模式(反模式)
  • 保持服务轻量级

2. 常见陷阱

// 错误示例:捕获Scoped服务到Singleton中
services.AddSingleton<IBackgroundService>(provider => {var scopedService = provider.GetRequiredService<IScopedService>(); // 危险!return new BackgroundService(scopedService);
});// 正确做法:使用IServiceScopeFactory
services.AddSingleton<IBackgroundService>(provider => {var scopeFactory = provider.GetRequiredService<IServiceScopeFactory>();return new BackgroundService(scopeFactory);
});

八、性能优化

1. 避免过度注入

// 不好:注入过多服务
public class OrderService(ILogger logger,IEmailService emailService,ISmsService smsService,IRepository repo,ICache cache,IConfig config)
{// ...
}// 改进:使用聚合服务
public class OrderService(ILogger logger,INotificationService notification,IOrderInfrastructure infra)
{// ...
}

2. 编译时注入

[RegisterTransient(typeof(IMyService))]
public class MyService : IMyService
{// ...
}// 使用Source Generator自动生成注册代码
static partial class ServiceRegistration
{static partial void AddGeneratedServices(IServiceCollection services){services.AddTransient<IMyService, MyService>();}
}

.NET的依赖注入系统是框架的核心基础设施,理解其原理和实现方式有助于编写更可测试、更松耦合的应用程序。

在这里插入图片描述

相关文章:

  • 凡科做的网站怎么改壁纸百度权重3的网站值多少
  • b2b网站建设公司b站新人视频怎么推广
  • 上饶专业做网站建设自助建站系统源码
  • 做网站开发的是不是程序员网站外链怎么发布
  • 洛阳网站建设 培训长沙正规关键词优化价格从优
  • 珠峰网站建设qq群推广平台
  • Ubuntu下交叉编译工业相机库aravis到OpenHarmony(aarch64)使用
  • GaussDB实例级自动备份策略:构建数据安全的“自动防护网”
  • 终止分区表变更操作时误删数据字典缓存导致MySQL崩溃分析
  • 卡萨帝发布AI深度科技:实现从守护生活到守护文明的升级
  • 人生笔记Real:记录生活点滴,守护珍贵记忆
  • MiniMax-M1混合MoE大语言模型(本地运行和私有化搭建)
  • 微服务思想与C++服务化框架
  • Hadoop之HDFS
  • DeepSeek在数据分析与科学计算中的革命性应用
  • 实战使用 Docker Compose 搭建 Redis Cluster 集群
  • 盖雅工场 2025 香港 SAP NOW 大会深度解析:AI 重构亚太劳动力管理数字化生态
  • Tauri(2.5.1)+Leptos(0.8.2)开发自用桌面小程序--DeepSeek辅助编程(俄罗斯方块)
  • 自用006
  • 单片机常用通信外设特点及通信方式对比表
  • Spark Streaming 与 Flink 实时数据处理方案对比与选型指南
  • QUdpScoket 组播实现及其中的踩坑点记录
  • 云原生与人工智能的融合:从弹性架构到智能运维的IT新范式
  • 华为云Flexus+DeepSeek征文 | 掌握高效开发:利用华为云ModelArts Studio在VS Code中配置Cline AI编程助手
  • [AI工具]Fish-speech部署教程(环境搭建记录)
  • spring boot项目整合百度翻译