从本文开始,我们将逐一实践Castle IOC中的Facility,在前面我们说过,Facility它是带有注入性质的。有时我们会遇到这样的问题,当一个组件满足一定的依赖关系之后,让它自动运行,比如说启动一个窗体或者启动某种服务,本文我们就来看如何使用Startable Facility让一个实现了接口IStartable的组件自动运行,以及不实现IStartable接口的组件如何在满足依赖后自动运行。
主要内容
1.Startable Facility概述
2.实现IStartable接口使用详解
3.不实现IStartable接口使用
摘要:从本文开始,我们将逐一实践Castle IOC中的Facility,在前面我们说过,Facility它是带有注入性质的。有时我们会遇到这样的问题,当一个组件满足一定的依赖关系之后,让它自动运行,比如说启动一个窗体或者启动某种服务,本文我们就来看如何使用Startable Facility让一个实现了接口IStartable的组件自动运行,以及不实现IStartable接口的组件如何在满足依赖后自动运行。
主要内容
1.Startable Facility概述
2.实现IStartable接口使用详解
3.不实现IStartable接口使用
一.Startable Facility概述
在开始使用Startable Facility之前,我们先了解一下它做了什么事情,它可以让一个组件在满足依赖关系之后自动启动或者停止。官方网站中提供的Startable Facility的有关信息:
Facility Information
|
Uses Proxy
|
No
|
Requires Configuration
|
No
|
Uses Attributes
|
No
|
Version
|
Beta 2
|
二.实现IStartable接口使用详解
Startable Facility的使用可以说是非常地简单,只要我们的组件实现了IStartable接口就可以了。现在我们还有一个Program类,它专门控制Server的启动和停止,我们希望在它的依赖关系满足后,让Server自动启动。很简单,我们让Program类实现IStartable接口:

/**//// <summary>

/// Author:Terrylee

/// Date:2006年4月28日

/// From:http://terrylee.cnblogs.com

/// </summary>
public class Program : IStartable


{
private Server _server;

public Program(Server server)

{
this._server = server;
}

public void Start()

{
_server.Start();
}

public void Stop()

{
_server.Stop();
}
}
注意这个里面的Start()和Stop()方法就是要实现接口中的方法,我们在Start()方法中启动服务器,在Stop()方法中停止服务器。并且这个类依赖于Server类,也就是要满足它的依赖关系,还需要有一个Server组件。服务器Server,它需要一个Host和Port:

/**//// <summary>

/// Author:Terrylee

/// Date:2006年4月28日

/// From:http://terrylee.cnblogs.com

/// </summary>
public class Server


{
private string _host;

private int _port;

public Server(string host,int port)

{
this._host = host;

this._port = port;
}

public void Start()

{
Console.WriteLine("Server {0}:{1} Start
",_host,_port);

Console.ReadLine();
}

public void Stop()

{
Console.WriteLine("Server {0}:{1} Stop
",_host,_port);

Console.ReadLine();
}
}
同时对于这个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是不需要配置文件的。好了,现在我们来看客户程序的使用:

/**//// <summary>

/// Author:Terrylee

/// Date:2006年4月28日

/// From:http://terrylee.cnblogs.com

/// </summary>
public class App


{
public static void Main()

{
//创建Windsor容器

IWindsorContainer container = new WindsorContainer(new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml"));

//添加Facility

container.AddFacility("startable", new StartableFacility());

//添加Program组件 (A)

container.AddComponent("program", typeof(Program));

//添加Server组件(B)

container.AddComponent("server", typeof(Server));
}
}
可以看到,在这个过程中,没有一点多余的代码,首先添加Startable Facility到容器中,然后添加Program组件,即执行到上面的A句的时候,因为还没有添加Server组件,不满足它的依赖关系,所以它无法启动,当添加完Server组件后,即执行了B句后,满足了它的依赖关系,这个它才会自动执行。
三.不实现IStartable接口使用
这是个很多人都忽略的问题,开始时我一直认为只有实现了IStartable接口才能使用Startable Facility,后来我在读它的源码时发现了一个问题,它不仅仅是判断组件是否实现了这个接口,如果组件有Startable特性也可以在满足依赖性后自动启动,这个在下一篇原理分析篇中我会介绍到。然后我就去查找这方面的资料,很可惜的网上从来没有介绍这种使用方法,我从它的TestCase找到了一点下面的代码,供有兴趣的朋友参考一下:
没有实现IStartable接口的组件:
[Transient]

public class NoInterfaceStartableComponent


{
private bool _Started = false;

private bool _Stopped = false;

public void Start()

{
_Started = true;
}

public void Stop()

{
_Stopped = true;
}

public bool Started

{

get
{ return _Started; }
}

public bool Stopped

{

get
{ return _Stopped; }
}
}
测试代码:
[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(二)
参考资料
Castle的官方网站http://www.castleproject.org
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?