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 = ; i < ; 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 = ; i < ; 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");

autofac学习的更多相关文章

  1. autofac 学习记录

    builder.RegisterModule(new ConfigurationSettingsReader()); 需要注册上面一句才能读到.config里的节点,xml配置方式如下 <con ...

  2. AutoFac学习摘要

    依赖注入(控制反转)常见的依赖注入工具:AutoFac,Spring.Net,Unity等依赖注入的方式:1.通过构造函数进行注入2.通过属性进行注入 注意:在项目中AutoFac的注入有两次,第一次 ...

  3. Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency

    InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...

  4. Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency 【转载】

    InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...

  5. IoC容器Autofac学习笔记

    一.一个没有使用IoC的例子 IoC的全称是Inversion of Control,中文叫控制反转.要理解控制反转,可以看看非控制反转的一个例子. public class MPGMovieList ...

  6. 依赖反转Ioc和unity,autofac,castle框架教程及比较

    1.依赖倒置的相关概念 http://www.cnblogs.com/fuchongjundream/p/3873073.html IoC模式(依赖.依赖倒置.依赖注入.控制反转) 2.依赖倒置的方式 ...

  7. 【框架学习与探究之依赖注入--Autofac】

    声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7603642.html 序 同样的又是一个双11如期而至,淘宝/天猫实时数据显示,开场3分钟总交易额突破 ...

  8. autofac 初步学习

    //数据处理接口 public interface IDal<T> where T : class { void Insert (T model); void Update(T model ...

  9. .Net Core 学习之路-AutoFac的使用

    本文不介绍IoC和DI的概念,如果你对Ioc之前没有了解的话,建议先去搜索一下相关的资料 这篇文章将简单介绍一下AutoFac的基本使用以及在asp .net core中的应用 Autofac介绍 组 ...

随机推荐

  1. 网络模拟工具Clumsy

    Clumsy 是一款小巧而功能强大的开源弱网模拟工具,它能在windows平台下人工造成不稳定的网络状况,方便你调试应用程序在极端网络状况下的表现. 你可以选择 clumsy 提供的功能来有目的性的调 ...

  2. Berkeley DB 使用经验总结

    作者:陈磊 NoSQL是现在互联网Web2.0时代备受关注的技术之一,被用来存储大量的非关系型的数据.Berkeley DB作为一款优秀的Key/Value存储引擎自然也在讨论之列.最近使用BDB来发 ...

  3. cuda by example

    int offset= x+y*dim   x 线程块内的线程索引 y 线程块索引 dim 线程块的维度   tid = threadIdx.x+blockIdx.x*blockDim.x 计算大于或 ...

  4. 2019.2.10考试T2, 多项式求exp+生成函数

    \(\color{#0066ff}{ 题目描述 }\) 为了减小文件大小,这里不写一堆题目背景了. 请写一个程序,输入一个数字N,输出N个点的森林的数量.点有标号. 森林是一种无向图,要求图中不能存在 ...

  5. DP【洛谷P2134】 百日旅行

    [洛谷P2134] 百日旅行 题目背景 重要的不是去哪里,而是和你在一起.--小红 对小明和小红来说,2014年7月29日是一个美好的日子.这一天是他们相识100天的纪念日. (小明:小红,感谢你2场 ...

  6. ubuntu14.04 搭建samba

        1.安装软件      sudo apt-get remove libwbclient0      sudo apt-get remove samba-common      sudo apt ...

  7. C语言把字符串转换为数字

    C当中有一些函数专门用于把字符串形式转换成数值形式. printf()函数和sprintf()函数 -->通过转换说明吧数字从数字形式转换为字符串形式: scanf()函数把输入字符串转换为数值 ...

  8. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

  9. maven set MAVEN_OPTS

    http://juvenshun.iteye.com/blog/240257 https://docs.alfresco.com/5.1/tasks/alfresco-sdk-install-mave ...

  10. hutool http+天气预报

    中国天气接口:http://www.weather.com.cn/data/sk/地址.html,只显示当天. sojson接口:http://t.weather.sojson.com/api/wea ...