[转]多个IoC容器适配器设计及性能测试和容器选择

1. 采用的IoC容器和版本

Autofac.2.6.3.862

Castle.Windsor.3.1.0

Spring.Core.2.0.0

2. 基础类库:服务类库和组件类库及相关的辅助类库

辅助类库:Demo.Common.dll

服务接口类库:Demo.Lib.dll

Oracle组件类库:Demo.Lib.Oracle.dll

Sql组件类库:Demo.Lib.Sql.dll

3. Autofac容器适配器

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);
}
} }
}

4. Castle.Windsor容器适配器

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);
}
} }
}

5. Spring.NET的容器适配器

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);
}
}
}
}

6. 测试结果

6.1. 单例模式

6.2. 瞬时(多例)模式

7. 测试结论

(1) 不管是单例还是多例,初始化的时间都差不多,Autofac略快

Autofac略快
 Spring和Windsor略慢,但两者相差不大

(2) 不管是单例还是多例,非初始化取数据时,取数据量大时,花费的时间也大

(3) 单例时,性能比较,Autofac略慢,但差异不大

Spring和Windsor花费时间差不多
 Autofac比较慢

(4) 多例时,性能比较,Autofac速度最快,也最稳定,差异较大

Autofac速度最快,相当稳定
 Windsor速度居中:是Autofac的1~2倍
 Spring速度最慢: 是Autofac的5~6倍左右

8. IoC容器选择结论(Autofac第一名)

A. 单例时,Autofac会略慢,但相差不大

B. 多例时,Autofac速度明显较快,Spring性能下降太厉害

C. 整体而言,Autofac的优劣较明显

D. 性能对比,Autofac > Windsor > Spring

9. Demo下载

点此下载

多个IoC容器适配器设计及性能测试(Castle.Windsor Autofac Spring.Core)的更多相关文章

  1. Spring源码解析-ioc容器的设计

    Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...

  2. Spring源码阅读:IOC容器的设计与实现(二)——ApplicationContext

    上一主题(查看)中,了解了IOC容器的基本概念,以及BeanFactory的设计与实现方式,这里就来了解一下ApplicationContext方式的实现. ApplicationContext 在S ...

  3. Spring IoC容器的设计——BeanFactory应用场景2

    1.BeanFactory接口设计了getBean方法,这个方法是使用IoC容器API的主要方法,通过这个方法,可以取得IoC容器中管理的Bean,Bean的取得是通过指定名字来索引的. 2.如果需要 ...

  4. Spring IoC容器的设计——BeanFactory应用场景

    1.BeanFactory提供的是最基本的IoC容器的功能,关于这些功能定义,我们可以在接口BeanFatory中看到. 2.BeanFactory接口定义了IoC容器最基本的容器的形式,并且提供了I ...

  5. Spring IoC容器的设计—3—次线

    这里涉及的是主要接口关系,而具体的IoC容器都是在这个接口体系下实现的,比如DefaultListableBeanFactory,这个基本IoC容器的实现就是实现了ConfigurableBeanFa ...

  6. Spring IoC容器的设计—2—主线

    第二条接口设计主线是,以ApplicationContext应用上下文接口为核心的接口设计,这里涉及的主要接口设计有,从BeanFactory到ListableBeanFactory,再到Applic ...

  7. Spring IoC容器的设计—1—主线

    IoC容器的接口设计图 下面对接口关系做一些简要的分析,可以依据以下内容来理解这张接口设计图. 从接口BeanFactory到HierarchicalBeanFactory,再到Configurabl ...

  8. Spring IoC容器与应用上下文的设计与实现

    一.前言 写这篇博文的主要目的如下: 通过相关类和接口分析IoC容器到底长什么样. 阐述笔者对Spring上下文和容器的理解. 介绍重要的类辅助理解SpringBoot的启动流程. 二.Spring ...

  9. BeanFactory容器的设计原理

    XmlBeanFactory设计的类继承关系 1.BeanFactory接口提供了使用IoC容器的规范.在这个基础上,Spring还提供了符合这个IoC容器接口的一系列容器的实现供开发人员使用. 2. ...

随机推荐

  1. qs文档翻译

    安装: npm install qs --save-dev 基本用法: let qs = require('qs'); let assert = require('assert'); //qs.par ...

  2. html5的拖拽dragAPI(如果看了API不懂,看看那三个案例就会恍然大悟)

    1.拖拽和释放定义: 拖拽:Drag; 释放:Drop; 2.源对象和目标对象: 3.相关API: ondragstart:源对象开始被拖动 ondrag:源对象被拖动的过程中 ondragend:源 ...

  3. java 多线程 19: ReentrantLock 与 Condition

    ReentrantLock ReentrantLock,一个可重入的互斥锁,它具有与使用synchronized方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大. Reentran ...

  4. JAVA-JSP内置对象之response对象实现页面跳转

    相关资料:<21天学通Java Web开发> response对象 实现页面跳转1.可以通过response对象的sendRedirect()方法设置页面重定向,从而实现页面跳转.2.这种 ...

  5. 【转】 Java中的变量赋值和参数传递

    原文地址:http://blog.csdn.net/whmii/article/details/3363667 变量赋值和参数传递是java中两个容易让人迷惑的问题.对于原始类型(primitives ...

  6. Thinkphp CURD中的where方法

    今天来给大家讲下查询最常用但也是最复杂的where方法,where方法也属于模型类的连贯操作方法之一,主要用于查询和操作条件的设置.where方法的用法是ThinkPHP查询语言的精髓,也是Think ...

  7. client version is higher than daemon version (client is v.1.29 daemon is v.1.22)

    安装好coreseek,建了索引,启动了服务,用php建了一个test.php,用于测试:<?phpinclude_once('sphinxapi.php');//向搜索引擎发起请求 $cl = ...

  8. STM32cube库配置双ADC的同步规则采样

    http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=605203&extra=page%3D&page ...

  9. linux用户和权限详解

    1.用户组说明 在使用useradd命令创建用户的时侯可以用-g 和-G 指定用户所属组和附属组.基本组:如果没有指定用户组,创建用户的时候系统会默认同时创建一个和这个用户名同名的组,这个组就是基本组 ...

  10. docker监控

    [编者的话]这篇文章作者是Usman,他是服务器和基础架构工程师,有非常丰富的分布式构建经验.该篇文章主要分析评估了五种Docker监控工具,包括免费的和不免费的:Docker Stats.CAdvi ...