哈尔滨网站设计有哪些步骤,wordpress主体公园,韶关营销网站开发,安康优质网站建设方案1.概述 autoFac的生命周期
序号名称说明1InstancePerDependency每次请求都创建一个新的对象2InstancePerLifetimeScope同一个Lifetime生成的对象是同一个实例3SingleInstance每次都用同一个对象
2.注 InstancePerLifetimeScope
同一个Lifetime生成的对象是同一个实例#x…1.概述 autoFac的生命周期
序号名称说明1InstancePerDependency每次请求都创建一个新的对象2InstancePerLifetimeScope同一个Lifetime生成的对象是同一个实例3SingleInstance每次都用同一个对象
2.注 InstancePerLifetimeScope
同一个Lifetime生成的对象是同一个实例这个理解起来有点不确定什么叫“同一个Lifetime生成的对象”就是生成过的几次比如在一个程序中只调用一次生成。
var builder new ContainerBuilder();builder.RegisterTypeA().AsIA().InstancePerLifetimeScope();IContainer container builder.Build();
只调用 一次上面的注册后面无论多少次获取都是一个对象。
IA a container.ResolveIA();
试验代码 class T2{static IContainer container;static T2(){var builder new ContainerBuilder();builder.RegisterTypeA().AsIA().InstancePerLifetimeScope();container builder.Build();}public void fun(){for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}}}
3.那么“同一个Lifetime生成的对象”指创建过一次那么这个创建指的是“InstancePerLifetimeScope”还是“Build”
于是有了下面的试验“InstancePerLifetimeScope”一次但是每次获取对象前都“Build”
builder new ContainerBuilder();builder.RegisterTypeA().AsIA().InstancePerLifetimeScope();
var container builder.Build();
结果报了如下异常 异常
System.InvalidOperationException:“Build() or Update() can only be called once on a ContainerBuilder 异常翻译
System.InvalidOperationException: Build()或Update()只能被ContainerBuilder调用一次
只能调用一次那么“InstancePerLifetimeScope”和“Build”的调用次数只能是相同的要么都同时一次要么都同时多次。
完整代码
class T3{static ContainerBuilder builder;static T3(){builder new ContainerBuilder();builder.RegisterTypeA().AsIA().InstancePerLifetimeScope();}public void fun(){var container builder.Build();for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}}}
4.另外两种生命周期都很好理解SingleInstance相当于单件“InstancePerDependency”相当于每次都创建。如果没有描述生命周默认也是这个builder.RegisterTypeA().AsIA()
InstancePerDependency(每次使用都是一个新的对象)
static void Main(string[] args){Console.WriteLine();var builder new ContainerBuilder();builder.RegisterTypeA().AsIA().InstancePerDependency();IContainer container builder.Build();for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}Console.ReadKey();} SingleInstance多次获取使用的是一个对象
static void Main(string[] args){Console.WriteLine();var builder new ContainerBuilder();builder.RegisterTypeA().AsIA().SingleInstance();IContainer container builder.Build();for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}Console.ReadKey();}
6.对比试验
生命周期对象中创建类中创建InstancePerDependency每次获取的都是新对象每次获取的都是新对象InstancePerLifetimeScope在创建对象的容器中是一个对象每次获取的都是新对象SingleInstance在创建对象的容器中是一个对象每次获取的都是新对象
这里创建指build
var builder new ContainerBuilder();builder.RegisterTypeA().AsIA())container builder.Build();
这里的使用指resolve
IA a container.ResolveIA();a.fun();
在对象中创建指在构造函数中创建每创建一个对象就创建一次。
在类中创建指在静态类中创建无论多少个类指创建一次。
代码
using Autofac;
using Autofac.Builder;
using System;
using System.Collections.Generic;namespace autoFac生命周期2
{class Program{static void Main(string[] args){Console.WriteLine();Program p new Program();p.main();Console.ReadKey();}private void main(){foreach (EnumLeftFun enumLeftFun in Enum.GetValues(typeof(EnumLeftFun))){T.enumLeftFunMy enumLeftFun;Console.WriteLine(\n------------enumLeftFun--------------------------------);test1();test2();}//test3();}private void test1(){Console.WriteLine( test1 每个对象创建一次);T1 t1 new T1();t1.fun();T1 t2 new T1();t2.fun();}private void test2(){Console.WriteLine( test2 每个类创建一次);T2 t21 new T2();t21.fun();T2 t22 new T2();t22.fun();}private void test3(){T3 t3 new T3();t3.fun();T3 t31 new T3();t31.fun();}class T2 : T{static IContainer container;static T2(){var builder new ContainerBuilder();T.leftSelect(builder.RegisterTypeA().AsIA());container builder.Build();}public void fun(){for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}}}class T3{static ContainerBuilder builder;static T3(){builder new ContainerBuilder();T.leftSelect(builder.RegisterTypeA().AsIA());}public void fun(){var container builder.Build();for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}}}class T1:T{IContainer container;public T1(){var builder new ContainerBuilder();leftSelect(builder.RegisterTypeA().AsIA());builder.RegisterTypeA().AsIA().SingleInstance();container builder.Build();}public void fun(){for (int i 0; i 3; i){IA a container.ResolveIA();a.fun();}}}delegate IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle delegateFun(IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle registrationBuilder);public enum EnumLeftFun{InstancePerDependency,InstancePerLifetimeScope,SingleInstance}class T{static DictionaryEnumLeftFun, delegateFun keyValuePairs new DictionaryEnumLeftFun, delegateFun();public static EnumLeftFun enumLeftFunMy;static T(){keyValuePairs.Add(EnumLeftFun.InstancePerDependency, InstancePerDependency);keyValuePairs.Add(EnumLeftFun.InstancePerLifetimeScope, InstancePerLifetimeScope);keyValuePairs.Add(EnumLeftFun.SingleInstance, SingleInstance);}static public IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle leftSelect(IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle registrationBuilder){return keyValuePairs[enumLeftFunMy](registrationBuilder);}static protected IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle InstancePerDependency(IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle registrationBuilder){return registrationBuilder.InstancePerDependency();}static protected IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle InstancePerLifetimeScope(IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle registrationBuilder){return registrationBuilder.InstancePerLifetimeScope();}static protected IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle SingleInstance(IRegistrationBuilderIA, ConcreteReflectionActivatorData, SingleRegistrationStyle registrationBuilder){return registrationBuilder.SingleInstance();}}}interface IA{void fun();}class A : IA{static int index 0;private int myIndex;public A(){this.myIndex index;}public void fun(){Console.WriteLine(myIndex: myIndex);}}
}运行结果
------------InstancePerDependency--------------------------------test1 每个对象创建一次
myIndex:1
myIndex:1
myIndex:1
myIndex:2
myIndex:2
myIndex:2test2 每个类创建一次
myIndex:3
myIndex:4
myIndex:5
myIndex:6
myIndex:7
myIndex:8------------InstancePerLifetimeScope--------------------------------test1 每个对象创建一次
myIndex:9
myIndex:9
myIndex:9
myIndex:10
myIndex:10
myIndex:10test2 每个类创建一次
myIndex:11
myIndex:12
myIndex:13
myIndex:14
myIndex:15
myIndex:16------------SingleInstance--------------------------------test1 每个对象创建一次
myIndex:17
myIndex:17
myIndex:17
myIndex:18
myIndex:18
myIndex:18test2 每个类创建一次
myIndex:19
myIndex:20
myIndex:21
myIndex:22
myIndex:23
myIndex:24 文章转载自: http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.blxor.com.gov.cn.blxor.com http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn