Autofac.2.6.3.862
Castle.Windsor.3.1.0
Spring.Core.2.0.0
辅助类库:Demo.Common.dll
服务接口类库:Demo.Lib.dll
Oracle组件类库:Demo.Lib.Oracle.dll
Sql组件类库:Demo.Lib.Sql.dll
using Autofac; using System; namespace IoC.Core.Adapter { public sealed class AutofacContainerAdapter : IContainerAdapter { public AutofacContainerAdapter() { Prepare(); } private IContainer container; public void Prepare() { var autofacContainerBuilder = new ContainerBuilder(); //autofacContainerBuilder.RegisterType<Singleton>() // .As<ISingleton>() // .SingleInstance(); //autofacContainerBuilder.RegisterType<Transient>() // .As<ITransient>(); //autofacContainerBuilder.RegisterType<Combined>() // .As<ICombined>(); this.container = autofacContainerBuilder.Build(); } public T Resolve<T>() { return this.container.Resolve<T>(); } public T Resolve<T>(string ServiceName) { return this.container.ResolveNamed<T>(ServiceName); } public void Dispose() { // Allow the container and everything it references to be disposed. this.container = null; } public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { if (!string.IsNullOrEmpty(ServiceName) && !container.IsRegisteredWithName(ServiceName, TService)) { var autofacContainerBuilder = new ContainerBuilder(); var item = autofacContainerBuilder.RegisterType(TImplementation); if (!string.IsNullOrEmpty(ServiceName)) { item = item.Named(ServiceName, TService); } else { item = item.As(TService); } switch (LifeCycle) { case LifeCycleType.Singleton: item = item.SingleInstance(); break; case LifeCycleType.Transient: item = item.InstancePerDependency(); break; case LifeCycleType.Request: item = item.InstancePerLifetimeScope(); break; } //if (!string.IsNullOrEmpty(ServiceName)) //{ // autofacContainerBuilder.RegisterType(TImplementation).Named(ServiceName, TService); //} //else //{ // autofacContainerBuilder.RegisterType(TImplementation).As(TService); //} //container = autofacContainerBuilder.Build(); autofacContainerBuilder.Update(container); } } public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { Type TService = Type.GetType(TServiceFull); Type TImplementation = Type.GetType(TImplementationFull); if (!string.IsNullOrEmpty(ServiceName) && !container.IsRegisteredWithName(ServiceName, TService)) { Register(TService, TImplementation, ServiceName, LifeCycle); } } public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle) { string ServiceName = TImplementation.GetType().Name; Register(TService, TImplementation, ServiceName, LifeCycle); } public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle) { if (!string.IsNullOrEmpty(TImplementationFull)) { string ServiceName = TImplementationFull; if (TImplementationFull.IndexOf(",") > 0) { ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(",")); } Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle); } } } }
using Castle.MicroKernel.Registration; using Castle.Windsor; using System; using System.Collections.Generic; namespace IoC.Core.Adapter { public sealed class WindsorContainerAdapter : IContainerAdapter { public WindsorContainerAdapter() { Prepare(); } private WindsorContainer container; public void Prepare() { this.container = new WindsorContainer(); //this.container.Register(Component.For<ISingleton>().ImplementedBy<Singleton>()); //this.container.Register(Component.For<ITransient>().ImplementedBy<Transient>().LifeStyle.Transient); //this.container.Register(Component.For<ICombined>().ImplementedBy<Combined>().LifeStyle.Transient); } public T Resolve<T>() { return this.container.Resolve<T>(); } public T Resolve<T>(string ServiceName) { return this.container.Resolve<T>(ServiceName); } public void Dispose() { // Allow the container and everything it references to be disposed. this.container = null; } private IDictionary<string, string> NameDict = new Dictionary<string, string>(); public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { if (!string.IsNullOrEmpty(ServiceName) && !NameDict.ContainsKey(ServiceName)) { var item = Component.For(TService).ImplementedBy(TImplementation); switch (LifeCycle) { case LifeCycleType.Singleton: item = item.LifestyleSingleton(); break; case LifeCycleType.Transient: item = item.LifestyleTransient(); break; case LifeCycleType.Request: item = item.LifestylePerWebRequest(); break; } if (!string.IsNullOrEmpty(ServiceName)) { item = item.Named(ServiceName); NameDict[ServiceName] = null; } container.Register(item); } } public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { if (!string.IsNullOrEmpty(ServiceName) && !NameDict.ContainsKey(ServiceName)) { Type TService = Type.GetType(TServiceFull); Type TImplementation = Type.GetType(TImplementationFull); Register(TService, TImplementation, ServiceName, LifeCycle); } } public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle) { string ServiceName = TImplementation.GetType().Name; Register(TService, TImplementation, ServiceName, LifeCycle); } public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle) { if (!string.IsNullOrEmpty(TImplementationFull)) { string ServiceName = TImplementationFull; if (TImplementationFull.IndexOf(",") > 0) { ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(",")); } Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle); } } } }
using Spring.Core; using Spring.Context; using Spring.Context.Support; using Spring.Objects.Factory.Support; using System; namespace IoC.Core.Adapter { public sealed class SpringContainerAdapter : IContainerAdapter { public SpringContainerAdapter() { Prepare(); } private GenericApplicationContext container; public void Prepare() { //this.container = Spring.Context.Support.ContextRegistry.GetContext(); container = new GenericApplicationContext(); } public T Resolve<T>() { return (T)container.GetObject(typeof(T).FullName); } public T Resolve<T>(string ServiceName) { return (T)container.GetObject<T>(ServiceName); } public void Dispose() { // Allow the container and everything it references to be disposed. this.container = null; } public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { if (!string.IsNullOrEmpty(ServiceName) && !container.IsObjectNameInUse(ServiceName)) { string vName = TImplementation.GetType().ToString(); string TImplementationFull = vName + ", " + TImplementation.Assembly.ToString(); IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory(); AbstractObjectDefinition defi = factory.CreateObjectDefinition(TImplementationFull, null, AppDomain.CurrentDomain); string SpringObjeLifeCycle = "singleton";//"prototype" "request"; switch (LifeCycle) { case LifeCycleType.Singleton: SpringObjeLifeCycle = "singleton"; break; case LifeCycleType.Transient: SpringObjeLifeCycle = "prototype"; break; case LifeCycleType.Request: SpringObjeLifeCycle = "request"; break; } defi.Scope = SpringObjeLifeCycle; //建立容器 //GenericApplicationContext tmpContainer = container as GenericApplicationContext; container.RegisterObjectDefinition(ServiceName, defi); } } public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient) { if (!string.IsNullOrEmpty(ServiceName) && !container.IsObjectNameInUse(ServiceName)) { IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory(); AbstractObjectDefinition defi = factory.CreateObjectDefinition(TImplementationFull, null, AppDomain.CurrentDomain); string SpringObjeLifeCycle = "singleton";//"prototype" "request"; switch (LifeCycle) { case LifeCycleType.Singleton: SpringObjeLifeCycle = "singleton"; break; case LifeCycleType.Transient: SpringObjeLifeCycle = "prototype"; break; case LifeCycleType.Request: SpringObjeLifeCycle = "request"; break; } defi.Scope = SpringObjeLifeCycle; string vFullName = Type.GetType(TImplementationFull).FullName; //建立容器 //GenericApplicationContext tmpContainer = container as GenericApplicationContext; container.RegisterObjectDefinition(vFullName, defi); } } public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle) { string ServiceName = TImplementation.GetType().Name; Register(TService, TImplementation, ServiceName, LifeCycle); } public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle) { if (!string.IsNullOrEmpty(TImplementationFull)) { string ServiceName = TImplementationFull; if (TImplementationFull.IndexOf(",") > 0) { ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(",")); } Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle); } } } }
Autofac略快
Spring和Windsor略慢,但两者相差不大
Spring和Windsor花费时间差不多
Autofac比较慢
Autofac速度最快,相当稳定
Windsor速度居中:是Autofac的1~2倍
Spring速度最慢: 是Autofac的5~6倍左右
点此下载