ASP.Net Core 的底层设计支持和使用依赖注入。APS.Net Core 应用程序可以利用内置的框架服务将他们注入到启动类的方法中,并且应用程序服务能够配置注入。由APS.Net Core 提供的默认服务容器提供了最小功能集,并不是要取代其他容器。依赖注入(Dependency injection,DI)是一种实现对象及其合作者或依赖项之间松散耦合的技术。

服务生命周期

在我们讨论如何在实践中进行注入之前,了解什么是服务生命周期至关重要。当一个组件通过依赖注入请求另一个组件时,它所接收的实例是否对该组件的实例来说是唯一的,这取决于它的生命周期。设置生命周期从而决定组件实例化的次数,以及组件是否共享。

在ASP.NET Core中,内置的DI容器有三种模式:

  • Transient
  • Scoped
  • Transient
    Transient每次请求时都会创建瞬态组件,并且永远不会共享。
    Scoped每个作用域创建一个实例,作用域是在对应用程序的每个请求上创建的,因此,任何注册为Scoped的组件每个请求都会创建一次。
    Singleton只会创建一个实例,该实例在需要它的所有组件之间共享。因此始终使用相同的实例。
    理解这一点非常重要,如果将组件A注册为单例,则它不能依赖于具有Scoped或Transient生命周期的组件。总而言之:
    组件不能依赖比自己的生命周期小的组件。

接口定义

public interface IOperation
    {
        Guid OperationId { get; }
    }

    public interface IOperationTransient : IOperation { }

    public interface IOperationScoped : IOperation { }

    public interface IOperationSingleton : IOperation { }

    public interface IOperationSingletonInstance : IOperation { }

实现定义的接口:

public class Operation : IOperation,
        IOperationTransient,
        IOperationScoped,
        IOperationSingleton,
        IOperationSingletonInstance
    {
        public Operation() : this(Guid.NewGuid()) {

        }

        public Operation(Guid id) {
            OperationId = id;
        }

        public Guid OperationId { get; private set; }
    }

定义服务:

public IOperationTransient TransientOperation { get; }
        public IOperationScoped ScopedOperation { get; }

        public IOperationSingleton SingletonOperation { get; }

        public IOperationSingletonInstance SingletonInstanceOperation { get; }

        public OperationService(IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationSingletonInstance singletonInstanceOperation) {
            TransientOperation = transientOperation;
            ScopedOperation = scopedOperation;
            SingletonOperation = singletonOperation;
            SingletonInstanceOperation = singletonInstanceOperation;
        }

服务注册

注册服务实在Startup类的ConfigureServices(IServiceCollection)方法中完成的。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddTransient<IOperationTransient, Operation>();
            services.AddScoped<IOperationScoped, Operation>();
            services.AddSingleton<IOperationSingleton, Operation>();
            services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.NewGuid()));
            services.AddTransient<OperationService, OperationService>();
        }

服务注入

在控制器中使用:

public class OperationsController : Controller
    {
        private readonly OperationService _operationService;
        private readonly IOperationTransient _transientOperation;
        private readonly IOperationScoped _scopedOperation;
        private readonly IOperationSingleton _singletonOperation;
        private readonly IOperationSingletonInstance _singletonInstanceOperation;
        private readonly ILogger<OperationsController> _logger;

        public OperationsController(OperationService operationService,
            IOperationTransient transientOperation,
            IOperationScoped scopedOperation,
            IOperationSingleton singletonOperation,
            IOperationSingletonInstance singletonInstanceOperation,
            ILogger<OperationsController> logger) {
            _operationService = operationService;
            _scopedOperation = scopedOperation;
            _transientOperation = transientOperation;
            _singletonOperation = singletonOperation;
            _singletonInstanceOperation = singletonInstanceOperation;
            _logger = logger;
        }
    }

Razor视图使用:
可以使用新的关键字@inject在Razor视图中注入组件:

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

参考链接

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐