AutoFac 生命周期

InstancePerDependency (默认的) 每一次Resolve创建一个不同对象出来

SingleInstance 单一的,同一个对象

InstancePerLifetimeScope 同一个生命周期获得一样的对象,不同周期有不同对象(想想httpContxt)

ContainerBuilder.Build() 是从IContainer中接口中继承 而IContainer从ILifetimeScope而来

ILifetimeScope 也可以自己构造 ,从现在IContainer 弹射更远的范围 IContainer.BeginLifetimeScope();

或这样的 (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)](ILifetimeScope 难道是key、value的方式存??,具体原码没有研究)

以下代码

 

 var builder = new ContainerBuilder();
            builder.RegisterType().As().Keyed("SqlDatabase").InstancePerDependency();
            builder.RegisterType().As().Keyed("MySqlDatabase").InstancePerLifetimeScope();
            builder.RegisterType().As().Keyed("OracleDatabase").SingleInstance();
            builder.RegisterType().WithParameter(ResolvedParameter.ForKeyed("SqlDatabase"));
            using (var container = builder.Build())
            {
                
                var sqlDatabase1 = container.ResolveKeyed("SqlDatabase");
                var sqlDatabase2 = container.ResolveKeyed("SqlDatabase");
                var or1 = container.ResolveKeyed("OracleDatabase");
                var or2 = container.ResolveKeyed("OracleDatabase");
                var m1 = container.ResolveKeyed("MySqlDatabase");
                var m2 = container.ResolveKeyed("MySqlDatabase");
                ILifetimeScope inner = container.BeginLifetimeScope();
                var s1 = inner.ResolveKeyed("SqlDatabase");
                var s2 = inner.ResolveKeyed("SqlDatabase");
                var m3 = inner.ResolveKeyed("MySqlDatabase");
                var m4 = inner.ResolveKeyed("MySqlDatabase");
                var o1 = inner.ResolveKeyed("OracleDatabase");
                var o2 = inner.ResolveKeyed("OracleDatabase");
                Debug.WriteLine(string.Format("container SqlDatabase={0}", object.ReferenceEquals(sqlDatabase1, sqlDatabase2)));
                Debug.WriteLine(string.Format("container MySqlDatabase={0}", object.ReferenceEquals(m1, m2)));
                Debug.WriteLine(string.Format("container OracleDatabase={0}", object.ReferenceEquals(or1, or2)));
                Debug.WriteLine(string.Format("inner. SqlDatabase ={0}", object.ReferenceEquals(s1, s2)));
                Debug.WriteLine(string.Format("inner MySqlDatabase={0}", object.ReferenceEquals(m3, m4)));
                Debug.WriteLine(string.Format("inner OracleDatabase={0}", object.ReferenceEquals(o1, o2)));
                Debug.WriteLine(string.Format("container MySqlDatabase 与inner MySqlDatabase={0} ", object.ReferenceEquals(m1, m4)));
                Debug.WriteLine(string.Format("container SqlDatabase 与inner SqlDatabase={0} ", object.ReferenceEquals(sqlDatabase1, s1)));
                Debug.WriteLine(string.Format("container OracleDatabase 与inner OracleDatabase={0} ", object.ReferenceEquals(or1, o1)));

无论是Autofac 还是spring.net 还是其它的IOC这样的工具,不是万能的,它仅仅是解决了类的创建的设计模式,类的创建时间和方式,与传统的配置文件创建方式(load assembly ,Activator)有很多优势,一,效率,(传统方式效率不高,无论emit还是delagate)二、有着生命周期,三、方便性,使用很方便

而在程 序结构上我们还需设计好自己结构的设计模式。个人意见,欢迎指正。

 

 

 

转载于:https://www.cnblogs.com/shouhongxiao/p/3520460.html

你可能感兴趣的:(设计模式,java)