第一个Castle程序 - 科宇百科(OpenSource知识百科)

来源:百度文库 编辑:神马文学网 时间:2024/04/28 02:15:10
第一个Castle程序
答应
RSS
修改时间: 2007/10/14 12:00 由admin - 编目为:Castle
»第一个Castle程序
[X] »第一个Castle程序
首先要取得Castle的相关文件,Castle可以到Castle项目的网站上下载:http://www.castleproject.org/castle/download.html。
写这篇文章时,Castle最新的版本是RC3,下载zip档案并解压缩之后,在Bin目录下就是使用Castle所需要的相关档案。接下来要练习的第一个Castle程序,要将Castle.Core.dll、Castle.DynamicProxy2.dll、 Castle.MicroKernel.dll、Castle.Windsor.dll引用到项目中。 现在假如我们有这样一个需求,开发一个组件,获取一个网页,取得网页的标题,以示意性的代码来实现。
1.新建一个C#工程WindsorSample,添加对以下Dll的引用Castle.Core.dll、Castle.DynamicProxy2.dll、 Castle.MicroKernel.dll、Castle.Windsor.dll。
2.编写服务,按照需求,我们要完成两项工作,下载网页内容和分析网页内容,获取网页的标题,我们先添加两个接口分别为IFileDownloader,ITitleScraper,这样的接口我们一般叫做服务,即实现了某种服务的接口。 public interface IFileDownloader{string Download(Uri file);}public interface ITitleScraper{string Scrape(string fileContents);}3. 编写组件: 仅仅有接口还不行,还需要实现了上面两个接口的具体实现类,这些类我们把它叫做组件。 public class HttpFileDownloader : IFileDownloader{public string Download(Uri file){WebClient client = new WebClient();client.Encoding = Encoding.UTF8;return client.DownloadString(file);}}public class StringParsingTitleScraper : ITitleScraper{public string Scrape(string fileContents){string title = string.Empty;int openingTagIndex = fileContents.IndexOf("");if(openingTagIndex != -1 && closingTagIndex != -1)title = fileContents.Substring(openingTagIndex, closingTagIndex - openingTagIndex).Substring(7);return title;}}public class HtmlTitleRetriever{private readonly IFileDownloader dowloader;private readonly ITitleScraper scraper;public HtmlTitleRetriever(IFileDownloader dowloader, ITitleScraper scraper){this.dowloader = dowloader;this.scraper = scraper;}public string GetTitle(Uri file){string fileContents = dowloader.Download(file);return scraper.Scrape(fileContents);}}HtmlTitleRetriever依赖于IFileDownloader和ITitleScraper,这里用的构造函数注入
4.编写配置文件编写配置文件,由于HtmlTitleRetriever构造函数中需要一个IFileDownloader的实例和ITitleScraper实例,组件可以通过代码添加也可以在配置文件中配置,可以编写一个配置文件,这个也可以放在应用程序配置文件中(Web.config或者App.config)。

5.使用Castle IOC容器。使用Castle IOC容器非常简单,基本上分为建立容器,加入组件,获取组件,使用组件几个步骤。//#define USE_KERNELusing System;using System.Configuration;using System.Net;using Castle.MicroKernel;using Castle.Windsor;using Castle.Windsor.Configuration.Interpreters;using System.Text;namespace WindsorSample{public class Program{private static void Main(){IWindsorContainer container = new WindsorContainer(new XmlInterpreter());HtmlTitleRetriever retriever = container.Resolve();Console.WriteLine(retriever.GetTitle(new Uri(ConfigurationManager.AppSettings"fileUri")));container.Release(retriever);}}}第一步:注册了一个Windsor容器;
第二步:向容器中注册IFileDownloader服务并告诉容器HttpFileDownloader实现了这个服务,注册ITitleScraper服务并告诉容器
StringParsingTitleScraper实现了这个服务,注册组件HtmlTitleRetriever,注册HtmlTitleRetriever时容器会发现这个服务依赖于其他的服务,它会自动去寻找,如果找不到这样的服务,则会抛出一个异常;
我们并没有使用new关键字创建一个具体实现类的实例,至此,一个简单的使用Castle IOC的过程就完成了。只有在真正需要HtmlTitleRetriever对象时,才会实际建立HtmlTitleRetriever实例,而不是在一开始建立IWindsorContainer时就建立HtmlTitleRetriever实例
上面演示了使用Castle的两种方式,一种是使用配置文件,另一种是使用代码。当然Castle也支持同时使用配置文件和代码。
几个重要的概念
1.服务
服务是一个个的接口,接口约定了服务,从而使随意替换服务的实现对使用接口服务的代码没有任何的影响。像我们上面例子中的IFileDownloader,ITitleScraper都是一个个服务,我们在这个例子中只是实现了一个分析网站上的页面的页面的标题,如果你要是实现ftp网站上一个HTML文件的标题,都必须要遵守IFileDownloader这个接口。 2.组件
简单来说组件是一个可重用的程序单元,它实现了某个接口,并仅仅只实现了这一个良好的接口。也就是说,组件是实现了某个服务接口的类。像上例中的HttpFileDownloader,StringParsingTitleScraper都是组件 3.自动装配
在上面的例子中,大家可能都已经注意到了,HtmlTitleRetriever依赖于IFileDownloader和ITitleScraper,我们无需自己编写XML配置文件来配置组件之间的依赖关系,这就是Castle IOC聪明的一个地方,它能够自动管理组件之间的依赖关系,而无需编写特定的xml config来配置,即自动装配的意思。