AddSingleton,AddScoped,AddTransient 三者的差异性测试

 

服务类型

同一个 HTTP 请求的范围

多个不同 HTTP 请求

Singleton Service

同一个实例

同一个实例

Scoped Service

同一个实例

新实例

Transient Service

新实例

新实例

 自己理解的测试代码,简单的仓储模式

    [Route("api/User")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IUserService  userService;
        INewService newService;
        ICompanyService companyService;
        public UserController(
            IUserService userService,
            INewService newService,
            ICompanyService companyService)
        {
            this.userService = userService;
            this.newService = newService;
            this.companyService = companyService;
        }

        /// <summary>
        /// services.AddSingleton<IUserService, UserService>();
        /// 单例模式,应用程序生命期内,永远一致:s1==s2
        /// </summary>
        /// <returns></returns>
        [HttpGet("UserID")]
        public IList<long> GetUserID()
        {
            var result = new List<long>();
            var s1 = userService.GetHashCode();//获取此次请求时的注入对象hashcode
            var s2 = HttpContext.RequestServices.GetService(typeof(IUserService)).GetHashCode();//再次获取此次请求时的注入对象hashcode
            result.Add(s1);
            result.Add(s2);
            result.Add(userService.GetUserID());
            return result;
        }

        /// <summary>
        /// services.AddScoped<INewService, NewService>();
        /// 同一个Http请求,注入的对象一致:s1==s2,
        /// 不同的htpp请求注入对象不一致:s1!=s2
        /// </summary>
        /// <returns></returns>
        [HttpGet("NewID")]
        public IList<long> GetNewID()
        {
            var result = new List<long>();
            var s1 = newService.GetHashCode();//获取此次请求时的注入对象hashcode
            var s2 = HttpContext.RequestServices.GetService(typeof(INewService)).GetHashCode();//再次获取此次请求时的注入对象hashcode
            result.Add(s1);
            result.Add(s2);
            return result;
        }

        /// <summary>
        ///  services.AddTransient<ICompanyService, CompanyService>();
        ///  同一个Http请求,注入的对象不一致: s1!=s2,
        ///  不同的htpp请求注入对象更加是不一致: s1!=s2
        /// </summary>
        /// <returns></returns>
        [HttpGet("CompanyID")]
        public IList<long> GetCompanyID()
        {
            var result = new List<long>();
            var s1 = companyService.GetHashCode();//获取此次请求时的注入对象hashcode
            var s2 = HttpContext.RequestServices.GetService(typeof(ICompanyService)).GetHashCode();//再次获取此次请求时的注入对象hashcode
            result.Add(s1);
            result.Add(s2);
            return result;
        }
    }

 startup.cs添加配置

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IUserRespository, UserRespository>();
            services.AddScoped<INewRespository, NewRespository>();
            services.AddTransient<ICompanyRespository, CompanyRespository>();
            services.ConfigureInvalidModelState();
            services.AddControllers();
        }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值