zoukankan      html  css  js  c++  java
  • autofac学习

    Instance Scope

    1、instance per dependency    (the default)

    builder.RegisterType<classes>();等价于

    builder.RegisterType<classes>().InstencePerDenpendency();

    每次都创建一个新的依赖

    2、single instance

    builder.RegisterType<classes>().SingleInstance();

    单例,每次都返回同一个

    3、instance per lifetime scope

    builder.RegisterType<classes>().InstancePerLifetimeScope();

    在同一个生命周期中返回同一个实例

    官方文档eg:

    using(var scope1 = container.BeginLifetimeScope())
    {
      for(var i = 0; i < 100; i++)
      {
        // Every time you resolve this from within this
        // scope you'll get the same instance.
        var w1 = scope1.Resolve<Worker>();
      }
    }
    
    using(var scope2 = container.BeginLifetimeScope())
    {
      for(var i = 0; i < 100; i++)
      {
        // Every time you resolve this from within this
        // scope you'll get the same instance, but this
        // instance is DIFFERENT than the one that was
        // used in the above scope. New scope = new instance.
        var w2 = scope2.Resolve<Worker>();
      }
    }

    scope1中每次循环返回同一个Worker实例

    scope2中每次循环返回同一个Worker实例

    但是,scope1中的实例和scoper2中的实例并不是同一个

    4、instance per matching lifetime scope

    没看懂

    builder.RegisterType<classes>.InstancePerMatchingLifetimeScope("my");

    5、instance per request

    每次请求返回一个实例(在这个请求的lifetime scope)

    per request是matching lifetime scope的一种

    InstancePerRequest()  最终执行Autofac.Core.Lifetime.MatchingScopeLifetimeTags.AutofacWebRequest  

    等价于:builder.RegisterType<classes>.InstancePerMatchingLifetimeScope("AutofacWebRequest");

  • 相关阅读:
    【SQL跟踪工具】SQL Profiler 跟踪器
    使用Fiddler调试手机端页面请求/抓包
    SQL 常用判断语句
    VS中常用快捷键
    博客园博客自动生成目录/目录索引
    BZOJ 1135 P3488 LYZ-Ice Skates 线段树+Hall
    BZOJ 4823 老C的方块
    POJ
    BZOJ 1299 [LLH邀请赛]巧克力棒
    BZOJ 2437 [Noi2011]兔兔与蛋蛋
  • 原文地址:https://www.cnblogs.com/fuyujian/p/4496112.html
Copyright © 2011-2022 走看看