本文主要是介绍autoFac 生命周期 试验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.概述 autoFac的生命周期
序号 | 名称 | 说明 |
1 | InstancePerDependency | 每次请求都创建一个新的对象 |
2 | InstancePerLifetimeScope | 同一个Lifetime生成的对象是同一个实例 |
3 | SingleInstance | 每次都用同一个对象 |
2.注 InstancePerLifetimeScope
同一个Lifetime生成的对象是同一个实例,这个理解起来有点不确定,什么叫“同一个Lifetime生成的对象”,就是生成过的几次,比如在一个程序中,只调用一次生成。
var builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>().InstancePerLifetimeScope();IContainer container = builder.Build();
只调用 一次,上面的注册,后面无论多少次获取,都是一个对象。
IA a = container.Resolve<IA>();
试验代码
class T2{static IContainer container;static T2(){var builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>().InstancePerLifetimeScope();container = builder.Build();}public void fun(){for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}}}
3.那么“同一个Lifetime生成的对象”指创建过一次,那么这个创建指的是“InstancePerLifetimeScope”还是“Build”
于是有了下面的试验,“InstancePerLifetimeScope”一次,但是每次获取对象前都“Build”
builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>().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.RegisterType<A>().As<IA>().InstancePerLifetimeScope();}public void fun(){var container = builder.Build();for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}}}
4.另外两种生命周期,都很好理解,"SingleInstance"相当于单件,“InstancePerDependency”相当于每次都创建。(如果没有描述生命周,默认也是这个:builder.RegisterType<A>().As<IA>())
InstancePerDependency(每次使用都是一个新的对象)
static void Main(string[] args){Console.WriteLine();var builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>().InstancePerDependency();IContainer container = builder.Build();for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}Console.ReadKey();}
SingleInstance(多次获取使用的是一个对象)
static void Main(string[] args){Console.WriteLine();var builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>().SingleInstance();IContainer container = builder.Build();for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}Console.ReadKey();}
6.对比试验
生命周期 | 对象中创建 | 类中创建 | |
InstancePerDependency | 每次获取的都是新对象 | 每次获取的都是新对象 | |
InstancePerLifetimeScope | 在创建对象的容器中是一个对象 | 每次获取的都是新对象 | |
SingleInstance | 在创建对象的容器中是一个对象 | 每次获取的都是新对象 |
这里创建指:build
var builder = new ContainerBuilder();builder.RegisterType<A>().As<IA>())container = builder.Build();
这里的使用指:resolve
IA a = container.Resolve<IA>();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.RegisterType<A>().As<IA>());container = builder.Build();}public void fun(){for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}}}class T3{static ContainerBuilder builder;static T3(){builder = new ContainerBuilder();T.leftSelect(builder.RegisterType<A>().As<IA>());}public void fun(){var container = builder.Build();for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}}}class T1:T{IContainer container;public T1(){var builder = new ContainerBuilder();leftSelect(builder.RegisterType<A>().As<IA>());builder.RegisterType<A>().As<IA>().SingleInstance();container = builder.Build();}public void fun(){for (int i = 0; i < 3; i++){IA a = container.Resolve<IA>();a.fun();}}}delegate IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> delegateFun(IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder);public enum EnumLeftFun{InstancePerDependency,InstancePerLifetimeScope,SingleInstance}class T{static Dictionary<EnumLeftFun, delegateFun> keyValuePairs = new Dictionary<EnumLeftFun, delegateFun>();public static EnumLeftFun enumLeftFunMy;static T(){keyValuePairs.Add(EnumLeftFun.InstancePerDependency, InstancePerDependency);keyValuePairs.Add(EnumLeftFun.InstancePerLifetimeScope, InstancePerLifetimeScope);keyValuePairs.Add(EnumLeftFun.SingleInstance, SingleInstance);}static public IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> leftSelect(IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder){return keyValuePairs[enumLeftFunMy](registrationBuilder);}static protected IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> InstancePerDependency(IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder){return registrationBuilder.InstancePerDependency();}static protected IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> InstancePerLifetimeScope(IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder){return registrationBuilder.InstancePerLifetimeScope();}static protected IRegistrationBuilder<IA, ConcreteReflectionActivatorData, SingleRegistrationStyle> SingleInstance(IRegistrationBuilder<IA, 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
这篇关于autoFac 生命周期 试验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!