autofac的生命周期

autofac生命周期,寿命范围是一次性的,它们跟踪组件处理,完成后可以由GC回收

Instance Per Dependency 每次都会返回一个新的实例,并且这是默认的生命周期。

{
//瞬时生命周期-每一期获取对象都是一个全新的实例
ContainerBuilder builder = new ContainerBuilder();
 builder.RegisterType().As().InstancePerDependency();
 IContainer con = builder.Build();
 ITestServiceA test1 = con.Resolve();//获取服务
 ITestServiceA test2 = con.Resolve();//获取服务
Console.WriteLine(object.ReferenceEquals(test1,test2));//false
}

 Single Instance单例,所有服务请求都将会返回同一个实例。

//单例-在整个生命周期中对象永远都是同一实例
ContainerBuilder builder = new ContainerBuilder();
 builder.RegisterType().As().SingleInstance();
 IContainer con = builder.Build();
 ITestServiceA test1 = con.Resolve();//获取服务
 ITestServiceA test2 = con.Resolve();//获取服务
Console.WriteLine(object.ReferenceEquals(test1,test2));//true
}

 InstancePerLifetimeScope 在一个嵌套语句块中,只会返回一个实例。

//每个生命周期范围一个实例,不同生命周期范围实例不同
   ContainerBuilder containerBuilder=new ContainerBuilder();
containerBuilde.rRegisterType().As().InstancePerLifetimeScope()
IContainer container=containerBuilder.Build();
ITestServiceA test1=null;
ITestServiceA test2=null;
using(var scope=container.BeginLifetimeScope()){  //生命周期范围1
    ITestServiceA testservice1=scope.Resolve();
    ITestServiceA testservice2=scope.Resolve();
Console.WriteLine(object.ReferenceEquals(testservice1,testservice2));//true
    test1=testservice1;
}
using(var scope1=container.BeginLifetimeScope()){ //生命周期范围2
    ITestServiceA testservice3=scope1.Resolve();
    ITestServiceA testservice4=scope1.Resolve();
    Console.WriteLine(object.ReferenceEquals(testservice3,testservice4));//true
     test2=testservice3;
}
 Console.WriteLine(object.ReferenceEquals(test1,test2));//false

 InstancePerMatchingLifetimeScope当您创建嵌套的生存期范围时,您可以“标记”或“命名”范围。具有每匹配生命周期范围的组件每个嵌套生命周期范围最多只有一个实例与给定名称匹配。这允许您创建一种“范围单例”,其中嵌套的生命周期范围可以在不声明全局共享实例的情况下共享某个组件的实例。

#每个匹配生命周期范围的实例1
       {
                ContainerBuilder containerBuilder = new ContainerBuilder();
                containerBuilder.RegisterType().As().InstancePerMatchingLifetimeScope("zx");
                IContainer container = containerBuilder.Build();
                ITestServiceA test1 = null;
                ITestServiceA test2 = null;
                using (var scope = container.BeginLifetimeScope("zx"))
                {  //生命周期范围1
                    ITestServiceA testservice1 = scope.Resolve();
                    using (var scope2 = container.BeginLifetimeScope())
                    {  //生命周期范围1
                        ITestServiceA testservice12 = scope2.Resolve();

                        bool isok1 = object.ReferenceEquals(testservice1, testservice12);
                        Console.WriteLine(isok1);//true

                    }
                    test1 = testservice1;
                }
                using (var scope1 = container.BeginLifetimeScope("zx"))
                { //生命周期范围2
                    ITestServiceA testservice3 = scope1.Resolve();
                    ITestServiceA testservice4 = scope1.Resolve();
                    bool isok = object.ReferenceEquals(testservice3, testservice4);
                    Console.WriteLine(isok);//true
                    test2 = testservice3;
                }
                bool isok2 = object.ReferenceEquals(test1, test2);
                Console.WriteLine(isok2);//false
            }
        }

你可能感兴趣的:(c#,开发,c#)