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<SqlDatabase>().As<IDatabase>().Keyed<IDatabase>("SqlDatabase").InstancePerDependency();
            builder.RegisterType<MySqlDatabase>().As<IDatabase>().Keyed<IDatabase>("MySqlDatabase").InstancePerLifetimeScope();
            builder.RegisterType<OracleDatabase>().As<IDatabase>().Keyed<IDatabase>("OracleDatabase").SingleInstance();
            builder.RegisterType<DatabaseManager>().WithParameter(ResolvedParameter.ForKeyed<IDatabase>("SqlDatabase"));
            using (var container = builder.Build())
            {
                
                var sqlDatabase1 = container.ResolveKeyed<IDatabase>("SqlDatabase");
                var sqlDatabase2 = container.ResolveKeyed<IDatabase>("SqlDatabase");
                var or1 = container.ResolveKeyed<IDatabase>("OracleDatabase");
                var or2 = container.ResolveKeyed<IDatabase>("OracleDatabase");
                var m1 = container.ResolveKeyed<IDatabase>("MySqlDatabase");
                var m2 = container.ResolveKeyed<IDatabase>("MySqlDatabase");
                ILifetimeScope inner = container.BeginLifetimeScope();
                var s1 = inner.ResolveKeyed<IDatabase>("SqlDatabase");
                var s2 = inner.ResolveKeyed<IDatabase>("SqlDatabase");
                var m3 = inner.ResolveKeyed<IDatabase>("MySqlDatabase");
                var m4 = inner.ResolveKeyed<IDatabase>("MySqlDatabase");
                var o1 = inner.ResolveKeyed<IDatabase>("OracleDatabase");
                var o2 = inner.ResolveKeyed<IDatabase>("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)二、有着生命周期,三、方便性,使用很方便

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

 

 

 

posted @ 2014-01-15 11:28  一点点水  阅读(3989)  评论(1编辑  收藏  举报