• AddTransient,AddScope和AddSingleton 有什么不同?


    我们先来创建几个接口
    using System;

    namespace DependencyInjectionSample.Interfaces
    {
    public interface IOperation
    {
    Guid OperationId { get; }
    }

    public interface IOperationTransient : IOperation
    {
    }
    public interface IOperationScoped : IOperation
    {
    }
    public interface IOperationSingleton : IOperation
    {
    }
    public interface IOperationSingletonInstance : IOperation
    {
    }
    }
    然后在服务中依赖注入上面的接口


    services.AddTransient<IOperationTransient, Operation>();
    services.AddScoped<IOperationScoped, Operation>();
    services.AddSingleton<IOperationSingleton, Operation>();
    services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));
    services.AddTransient<OperationService, OperationService>();
    注意IOperationSingletionInstance和其他的依赖注入不同,它进行了显示实例化。

    创建一个服务
    using DependencyInjectionSample.Interfaces;

    namespace DependencyInjectionSample.Services
    {
    public class OperationService
    {
    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 instanceOperation)
    {
    TransientOperation = transientOperation;
    ScopedOperation = scopedOperation;
    SingletonOperation = singletonOperation;
    SingletonInstanceOperation = instanceOperation;
    }
    }
    }
    创建一个控制器
    using DependencyInjectionSample.Interfaces;
    using DependencyInjectionSample.Services;
    using Microsoft.AspNetCore.Mvc;

    namespace DependencyInjectionSample.Controllers
    {
    public class OperationsController : Controller
    {
    private readonly OperationService _operationService;
    private readonly IOperationTransient _transientOperation;
    private readonly IOperationScoped _scopedOperation;
    private readonly IOperationSingleton _singletonOperation;
    private readonly IOperationSingletonInstance _singletonInstanceOperation;

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

    public IActionResult Index()
    {
    // viewbag contains controller-requested services
    ViewBag.Transient = _transientOperation;
    ViewBag.Scoped = _scopedOperation;
    ViewBag.Singleton = _singletonOperation;
    ViewBag.SingletonInstance = _singletonInstanceOperation;

    // operation service has its own requested services
    ViewBag.Service = _operationService;
    return View();
    }
    }
    }
    两个不同的请求结果

    总结:

    Transient创建的对象总是不同的,每一个服务和每个控制器都创建一个不同的实例。

    Scoped创建的对象在同一个请求会话时是相同的,每一个不同的会话创建一个不同的实例。

    Singleton创建的对象在所有的地方所有的请求会话创建的都是不同的。

    原文:https://blog.csdn.net/loongsking/article/details/79964352

  • 相关阅读:
    年纪大了,要学会控制自己的欲望
    java为什么要用类型擦除实现泛型?--c++,java,c# 的泛型是如何实现的
    面向对象:类的成员---只是特定(类的)命名空间的全局变量(函数)而已
    人的意志与自然法则—人的意志与人造事物
    所有的思考都有两个目的:建模降熵
    控制论、系统论与熵
    秩序(也常简称为序)是系统的本质特征之一
    控制论:基于全局信息和整体目标、整体规则的系统结构和状态维护
    人类社会的全部认识成果可以概括为三大领域的科学,即自然科学、社会科学和思维科学
    “逻辑”的古希腊词源
  • 原文地址:https://www.cnblogs.com/littlewrong/p/10578964.html
Copyright © 2020-2023  润新知