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. JAVA进阶----ThreadPoolExecutor机制(转)

    http://825635381.iteye.com/blog/2184680 ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.co ...

  2. UDP socket

    1.Udp :User Datagram Protocol(Udp)用户数据报文协议. 2.适用于局域网的主机间通信. 3.使用方法 1:创建Socket OS_Socket local(127.0. ...

  3. P2245 星际导航 瓶颈路

    \(\color{#0066ff}{ 题目描述 }\) sideman 做好了回到 \(\text{Gliese}\) 星球的硬件准备,但是 \(\text{sideman}\) 的导航系统还没有完全 ...

  4. DockerFile一键搭建环境(一)

    点击查看文件详情 FROM centos:7 COPY --chown=root:root nginx /etc/init.d/ Run set -ex \  && yum insta ...

  5. linux文件系统相关概念

    struct task_struct { ......................... struct mm_struct*mm;//内存描述符的指针 struct files_struct *f ...

  6. select和epoll原理和区别

    对于select和poll,其主要原理跟epoll不同 poll和select的共同点就是,对全部指定设备(fd)都做一次poll,当然这往往都是还没有就绪的,那就会通过回调函数把当前进程注册到设备的 ...

  7. Qt 学习之路 2(2):Qt 简介

    Home / Qt 学习之路 2 / Qt 学习之路 2(2):Qt 简介 Qt 学习之路 2(2):Qt 简介  豆子  2012年8月21日  Qt 学习之路 2  43条评论 Qt 是一个著名的 ...

  8. 数据结构java学习(三)循环队列

    @TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 \(\color{black}{队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素}\) g ...

  9. C语言值拷贝传递机制

    当参数是常量,变量,或表达式时,传递的数据就是这些数据对象所具有的内容,这种方式称为数值参数传递方式(简称传值方式).如果函数调用时所传递的实参是数据对象在内存中的存储单元的首地址值,这种方式称为地址 ...

  10. Django 11 form表单(状态保持session、form表单及注册实现)

    Django 11 form表单(状态保持session.form表单及注册实现) 一.状态保持 session 状态保持 #1.http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状 ...