Castle IOC容器实践之Startable Facility(一)
2006-05-07 23:35
507 查看
摘要:从本文开始,我们将逐一实践Castle IOC中的Facility,在前面我们说过,Facility它是带有注入性质的。有时我们会遇到这样的问题,当一个组件满足一定的依赖关系之后,让它自动运行,比如说启动一个窗体或者启动某种服务,本文我们就来看如何使用Startable Facility让一个实现了接口IStartable的组件自动运行,以及不实现IStartable接口的组件如何在满足依赖后自动运行。
主要内容[/b]
1.Startable Facility概述
2.实现IStartable接口使用详解
3.不实现IStartable接口使用
一.Startable Facility概述[/b]
在开始使用Startable Facility之前,我们先了解一下它做了什么事情,它可以让一个组件在满足依赖关系之后自动启动或者停止。官方网站中提供的Startable Facility的有关信息:
二.实现IStartable接口使用详解[/b]
Startable Facility的使用可以说是非常地简单,只要我们的组件实现了IStartable接口就可以了。现在我们还有一个Program类,它专门控制Server的启动和停止,我们希望在它的依赖关系满足后,让Server自动启动。很简单,我们让Program类实现IStartable接口:
public class Program : IStartable
public class Server
<!--From:http://terrylee.cnblogs.com-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<components>
<component id="server">
<parameters>
<host>localhost</host>
<port>110</port>
</parameters>
</component>
</components>
</configuration>
需要注意的是这个配置文件跟Startable Facility没有任何关系,我们在配置文件中看不到任何和Startable Facility有关的代码。它只是一个普通的Castle IOC配置文件,因为我们在概述中已经说过了,Startable Facility是不需要配置文件的。好了,现在我们来看客户程序的使用:
public class App
[Transient]
public class NoInterfaceStartableComponent
[Test]
public void TestComponentWithNoInterface()
{
IKernel kernel = new DefaultKernel();
MutableConfiguration compNode = new MutableConfiguration("component");
compNode.Attributes["id"] = "b";
compNode.Attributes["startable"] = "true";
compNode.Attributes["startMethod"] = "Start";
compNode.Attributes["stopMethod"] = "Stop";
kernel.ConfigurationStore.AddComponentConfiguration("b", compNode);
kernel.AddFacility( "startable", new StartableFacility() );
kernel.AddComponent( "b", typeof(NoInterfaceStartableComponent) );
NoInterfaceStartableComponent component = kernel["b"] as NoInterfaceStartableComponent;
Assert.IsNotNull(component);
Assert.IsTrue( component.Started );
Assert.IsFalse( component.Stopped );
kernel.ReleaseComponent(component);
Assert.IsTrue( component.Stopped );
}
对于IKrnel大家可以自行修改为Castle.Windsor,这样也不失为一种使用Startable Facility的方法。
下篇:Castle IOC容器实践之Startable Facility(二)
参考资料[/b]
Castle的官方网站http://www.castleproject.org
主要内容[/b]
1.Startable Facility概述
2.实现IStartable接口使用详解
3.不实现IStartable接口使用
一.Startable Facility概述[/b]
在开始使用Startable Facility之前,我们先了解一下它做了什么事情,它可以让一个组件在满足依赖关系之后自动启动或者停止。官方网站中提供的Startable Facility的有关信息:
Facility Information | |
Uses Proxy | No |
Requires Configuration | No |
Uses Attributes | No |
Version | Beta 2 |
Startable Facility的使用可以说是非常地简单,只要我们的组件实现了IStartable接口就可以了。现在我们还有一个Program类,它专门控制Server的启动和停止,我们希望在它的依赖关系满足后,让Server自动启动。很简单,我们让Program类实现IStartable接口:
public class Program : IStartable
public class Server
<!--From:http://terrylee.cnblogs.com-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<components>
<component id="server">
<parameters>
<host>localhost</host>
<port>110</port>
</parameters>
</component>
</components>
</configuration>
需要注意的是这个配置文件跟Startable Facility没有任何关系,我们在配置文件中看不到任何和Startable Facility有关的代码。它只是一个普通的Castle IOC配置文件,因为我们在概述中已经说过了,Startable Facility是不需要配置文件的。好了,现在我们来看客户程序的使用:
public class App
[Transient]
public class NoInterfaceStartableComponent
[Test]
public void TestComponentWithNoInterface()
{
IKernel kernel = new DefaultKernel();
MutableConfiguration compNode = new MutableConfiguration("component");
compNode.Attributes["id"] = "b";
compNode.Attributes["startable"] = "true";
compNode.Attributes["startMethod"] = "Start";
compNode.Attributes["stopMethod"] = "Stop";
kernel.ConfigurationStore.AddComponentConfiguration("b", compNode);
kernel.AddFacility( "startable", new StartableFacility() );
kernel.AddComponent( "b", typeof(NoInterfaceStartableComponent) );
NoInterfaceStartableComponent component = kernel["b"] as NoInterfaceStartableComponent;
Assert.IsNotNull(component);
Assert.IsTrue( component.Started );
Assert.IsFalse( component.Stopped );
kernel.ReleaseComponent(component);
Assert.IsTrue( component.Stopped );
}
对于IKrnel大家可以自行修改为Castle.Windsor,这样也不失为一种使用Startable Facility的方法。
下篇:Castle IOC容器实践之Startable Facility(二)
参考资料[/b]
Castle的官方网站http://www.castleproject.org
相关文章推荐
- Castle IOC容器实践之Startable Facility(二)
- Castle IOC容器实践之Startable Facility(一)
- Castle IOC容器实践之Startable Facility(二)
- Castle IOC容器实践之Startable Facility(二)
- Castle IOC容器实践之TypedFactory Facility(二)
- Castle IOC容器实践之TypedFactory Facility(一)
- Castle IOC容器实践之TypedFactory Facility(二)
- Castle IOC容器实践之EnterpriseLibrary Configuration Facility
- Castle IOC容器实践之TypedFactory Facility(一)
- Castle IOC容器实践之TypedFactory Facility(二)
- Castle IOC容器快速入门
- Castle IOC容器快速入门
- Castle IOC容器内幕故事(上)
- Castle IOC容器构建配置详解(一)
- Castle IOC容器内幕故事(上)
- Castle IOC容器与Spring.NET配置之比较
- Castle IOC容器组件生命周期管理
- ASP.NET WebForm使用IoC容器(Spring.NET容器、Castle等等)
- Castle IOC容器构建配置详解(一)
- Castle IOC容器内幕故事(上)