Castle IOC容器组件生命周期管理
2006-04-26 08:24:00
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67683 |
摘要:Castle IOC容器为我们提供了不同的生命处理方式以及组件生命周期的处理,即组件装载,初始化,销毁时所表现的行为。如何去使用它们?本文讲对这一内容进行详细的阐述。
主要内容
1.生命处理方式
2.自定义生命处理方式
3.生命周期处理
一.生命处理方式
我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会使用Singleton模式。在Castle IOC中,它支持我们对于组件的实例进行控制,也就是说我们可以透明的管理一个组件拥有多少个实例。Castle IOC容器提供了如下几种生命处理方式:
l Singleton:一个组件只有一个实例被创建,所有请求的客户使用程序得到的都是同一个实例,同时这也是Castle IOC容器默认的一种处理方式。
l Transient:这种处理方式与我们平时使用new的效果是一样的,对于每次的请求得到的都是一个新的实例。
l PerThread:对于每一个线程来说是使用了Singleton,也就是每一个线程得到的都是同一个实例。
l Pooled:对象池的处理方式,对于不再需用的实例会保存到一个对象池中。
l Custom:自定义的生命处理方式。
我们可以通过以下两种方式来指定组件的生命处理方式,如果不指定,则为Singleton方式:
1.使用配置文件
<!--出处:http://terrylee.cnblogs.com-->![]() <?xml version="1.0" encoding="utf-8" ?>![]() <configuration>![]() <components>![]() <component id="comp1" lifestyle="transient">![]() <parameters>![]() <para>component1 para</para>![]() </parameters>![]() </component>>![]() </components>![]() </configuration>2.使用Attribute特性
//出处:http://terrylee.cnblogs.com![]() [Transient]![]() public class MyComponent { public MyComponent() { //![]() ![]() } public MyComponent(string _Str) { //![]() ![]() } }前面在Castle IOC的内幕故事中我们说过,组件生命方式是由一个叫做LifestyleModelInspector的Contributor来管理的。在LifestyleModelInspector中我们注意到有这样一段代码:
public virtual void ProcessModel(IKernel kernel, ComponentModel model) { if (!ReadLifestyleFromConfiguration(model)) { ReadLifestyleFromType(model); } }![]() protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model) { //![]() ![]() }![]() protected virtual void ReadLifestyleFromType(ComponentModel model) { //![]() ![]() }其中ReadLifestyleFromConfiguration()从配置文件读取,ReadLifestyleFromType()是从组件的特性读取。可以看到LifestyleModelInspector首先会去检查配置文件中的是否指定,如果已经指定了,就会直接返回,否则才去组件特性里面去查找。由此我们可以得出如下一条重要的结论:
如果同时在配置文件和组件的特性中指定组件生命处理方式,配置文件将覆盖类中特性指定的。
二.自定义生命处理方式
下面我们来看如何实现自定义的生命处理方式。在这之前,先来看一下生命处理方式中的类结构图:
图1可以看到,所有生命处理方式都实现了接口ILifestyleManager:
public interface ILifestyleManager : IDisposable { void Init(IComponentActivator componentActivator, IKernel kernel);![]() object Resolve();![]() void Release(object instance);![]() }所以要实现自定义的生命处理方式,只要实现接口IlifestyleManager就可以了,来看一下Castle IOC官方网站提供的一种生命处理方式,实现了对于Web应用程序中的每一次Request都创建一个Singleton实例:
public class PerWebRequestLifestyleManager : AbstractLifestyleManager { private string PerRequestObjectID = "PerRequestLifestyleManager_" + Guid.NewGuid().ToString(); ![]() public override object Resolve() { if(HttpContext.Current.Items[PerRequestObjectID] == null) { // Create the actual object![]() HttpContext.Current.Items[PerRequestObjectID] = base.Resolve(); ![]() } return HttpContext.Current.Items[PerRequestObjectID]; }![]() public override void Dispose() { ![]() } }对于自定义的生命处理方式,在使用配置文件和特性指定的时候又有些不同
1.使用配置文件
<!--出处:http://terrylee.cnblogs.com-->![]() <?xml version="1.0" encoding="utf-8" ?>![]() <configuration>![]() <components>![]() <component id="myComponent"![]() type="MyLib.MyComponent, MyLib"![]() lifestyle="custom"![]() customLifestyleType="MyLib.PerWebRequestLifestyleManager, MyLib">![]() <parameters>![]() <para>component1 para</para>![]() </parameters>![]() </component>![]() </components>![]() </configuration>2.使用Attribute特性
//出处:http://terrylee.cnblogs.com![]() [CustomLifestyle( typeof(PerWebRequestLifestyleManager ) )]![]() public class MyComponent { public MyComponent() { //![]() ![]() } //![]() ![]() }三.生命周期管理
Castle IOC同样是支持组件生命周期的管理,也就是在组件装载,初始化,销毁所出发的行为,分别对应三个接口:IInitializable,ISupportInitialize,IDisposable。这些接口被分为两组:Commission和Decommission:
Commission
l Castle.Model.IInitializable interface
l System.ComponentModel.ISupportInitialize
Decommission
l System.IDisposable
如果组件实现了这些接口,容器会自动在不同的生命周期调用他们。我们看下面这样一个例子:
//出处:http://terrylee.cnblogs.com![]() [Transient]![]() public class MyComponent : IInitializable, IDisposable { public MyComponent(string _para) { //![]() ![]() }![]() public void Initialize() { //![]() ![]() }![]() public void Dispose() { //![]() ![]() } }在我们使用组件时
//出处:http://terrylee.cnblogs.com![]() public class App { public static void Main() { IWindsorContainer container = new WindsorContainer(new XmlInterpreter("../../BasicUsage.xml") );![]() container.AddComponent( "myComponent", ![]() typeof(MyComponent));![]() // Initialize()方法会自动执行![]() MyComponent instince = container["myComponent"] as MyComponent;![]() // Dispose()方法会自动执行![]() container.Release(instince);![]() } }关于Castle IOC容器组件生命周期管理就介绍到这里了。 本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67683 本文出自 51CTO.COM技术博客 |









}
图
lihuijun
博客统计信息
热门文章
最新评论
友情链接
