.NetCore依赖注入(DI)之生命周期

打印 上一主题 下一主题

主题 949|帖子 949|积分 2847

在 .NET Core 中,依赖注入(Dependency Injection,DI)是一种实现控制反转(Inversion of Control,IoC)的技术,它通过将依赖对象注入到需要它们的对象中,来实现对象之间的解耦。依赖注入的生命周期决定了对象在应用程序中的创建和管理方式。常见的生命周期有三种:Transient(瞬态)Scoped(作用域)Singleton(单例)
1. Transient(瞬态)

定义:

每次请求时都会创建一个新的实例。
特点:


  • 每次注入时都会创建一个新的对象。
  • 对象的生命周期仅限于当前请求。
  • 适用于轻量级、无状态的服务。
使用场景:


  • 适用于每次请求都需要独立实例的场景,例如工具类、无状态服务等。
示例:
  1. public interface ITransientService
  2. {
  3.     Guid GetOperationId();
  4. }
  5. public class TransientService : ITransientService
  6. {
  7.     private readonly Guid _operationId;
  8.     public TransientService()
  9.     {
  10.         _operationId = Guid.NewGuid();
  11.     }
  12.     public Guid GetOperationId() => _operationId;
  13. }
复制代码
在 Startup.cs 中注册:
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3.     services.AddTransient<ITransientService, TransientService>();
  4. }
复制代码
在控制器中使用:
  1. public class HomeController : Controller
  2. {
  3.     private readonly ITransientService _transientService1;
  4.     private readonly ITransientService _transientService2;
  5.     public HomeController(ITransientService transientService1, ITransientService transientService2)
  6.     {
  7.         _transientService1 = transientService1;
  8.         _transientService2 = transientService2;
  9.     }
  10.     public IActionResult Index()
  11.     {
  12.         _transientService1.DoWork();
  13.         _transientService2.DoWork();
  14.         // 验证是否为不同实例
  15.         Console.WriteLine(_transientService1 == _transientService2);  // 输出:False
  16.         return Ok();
  17.     }
  18. }
复制代码
输出:
  1. Transient Service: Doing work...
  2.     Transient Service: Doing work...
  3.         False
复制代码
2. Scoped(作用域)

定义:

在同一个作用域内,对象是单例的;但在差别的作用域中,会创建新的实例。
特点:


  • 对象的生命周期与请求的作用域一致。
  • 适用于需要在请求范围内共享状态的服务。
使用场景:


  • 适用于需要在请求范围内共享状态的场景,例如数据库上下文、工作单元模式等。
示例:
  1. public interface IScopedService
  2. {
  3.     void DoWork();
  4. }
  5. public class ScopedService : IScopedService
  6. {
  7.     public void DoWork()
  8.     {
  9.         Console.WriteLine("Scoped Service: Doing work...");
  10.     }
  11. }
复制代码
在 Startup.cs 中注册
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3.     services.AddScoped<IScopedService, ScopedService>();
  4. }
复制代码
在控制器中使用:
  1. public class HomeController : Controller
  2. {
  3.     private readonly IScopedService _scopedService1;
  4.     private readonly IScopedService _scopedService2;
  5.     public HomeController(IScopedService scopedService1, IScopedService scopedService2)
  6.     {
  7.         _scopedService1 = scopedService1;
  8.         _scopedService2 = scopedService2;
  9.     }
  10.     public IActionResult Index()
  11.     {
  12.         _scopedService1.DoWork();
  13.         _scopedService2.DoWork();
  14.         // 验证是否为相同实例
  15.         Console.WriteLine(_scopedService1 == _scopedService2);  // 输出:True
  16.         return Ok();
  17.     }
  18. }
复制代码
输出:
  1. Scoped Service: Doing work...
  2.     Scoped Service: Doing work...
  3.         True
复制代码
3. Singleton(单例)

定义:

在整个应用程序生命周期中,只创建一个实例。
特点:


  • 对象的生命周期与应用程序的生命周期一致。
  • 适用于全局共享的服务,如设置管理、日志记载等。
使用场景:


  • 适用于需要全局共享的场景,例如设置管理、缓存、日志记载等。
示例:
  1. public interface ISingletonService
  2. {
  3.     void DoWork();
  4. }
  5. public class SingletonService : ISingletonService
  6. {
  7.     public void DoWork()
  8.     {
  9.         Console.WriteLine("Singleton Service: Doing work...");
  10.     }
  11. }
复制代码
在 Startup.cs 中注册:
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3.     services.AddSingleton<ISingletonService, SingletonService>();
  4. }
复制代码
在控制器中使用:
  1. public class HomeController : Controller
  2. {
  3.     private readonly ISingletonService _singletonService1;
  4.     private readonly ISingletonService _singletonService2;
  5.     public HomeController(ISingletonService singletonService1, ISingletonService singletonService2)
  6.     {
  7.         _singletonService1 = singletonService1;
  8.         _singletonService2 = singletonService2;
  9.     }
  10.     public IActionResult Index()
  11.     {
  12.         _singletonService1.DoWork();
  13.         _singletonService2.DoWork();
  14.         // 验证是否为相同实例
  15.         Console.WriteLine(_singletonService1 == _singletonService2);  // 输出:True
  16.         return Ok();
  17.     }
  18. }
复制代码
输出:
  1. Singleton Service: Doing work...
  2. Singleton Service: Doing work...
  3. True
复制代码
总结

生命周期定义特点使用场景Transient每次请求时创建新的实例每次注入时都会创建一个新的对象轻量级、无状态的服务,如工具类、无状态服务Scoped在同一个作用域内,对象是单例的对象的生命周期与请求的作用域一致需要在请求范围内共享状态的服务,如数据库上下文Singleton在整个应用程序生命周期中,只创建一个实例对象的生命周期与应用程序的生命周期一致全局共享的服务,如设置管理、日志记载通过合理选择依赖注入的生命周期,我们可以实现对象的灵活管理和高效使用,从而提高应用程序的性能和可维护性。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表