zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    nexus docker 私有镜像处理
    nexus 使用Raw Repositories 进行maven site 发布
    nexus && minio s3 存储私有镜像
    spring boot 使用spring.resources.static-locations 分离系统模版&&资源文件
    Tencent Server Web 安装试用
    docker could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
    Tencent Server Web(TSW) 腾讯开源的nodejs 基础设施
    Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
    13 Stream Processing Patterns for building Streaming and Realtime Applications
    Siddhi cep java 集成简单使用
  • 原文地址:https://www.cnblogs.com/littlewrong/p/10578964.html
Copyright © 2011-2022 走看看