摘要:EnterpriseLibrary Configuration Facility就好像是在容器和数据类之间的桥,让我们可以
轻松
得去读取和操作配置文件。熟悉Enterprise Library的人都知道,在Enterprise Library中有一个Configuration Application Block,它可以使我们方便的从各种存储中读写配置信息,通过EnterpriseLibrary Configuration Facility我们就可以像使用普通的组件那样去注册一个数据类,它会用configurationkey来映射到Enterprise Library的配置文件中。
主要内容:
1
.概述
2
.使用Facility
3
.原理浅析
一.概述
EnterpriseLibrary Configuration Facility
就好像是在容器和数据类之间的桥,让我们可以
轻松
得去读取和操作配置文件。熟悉Enterprise Library的人都知道,在Enterprise Library中有一个Configuration Application Block,它可以使我们方便的从各种存储中读写配置信息,通过EnterpriseLibrary Configuration Facility我们就可以像使用普通的组件那样去注册一个数据类,它会用configurationkey来映射到Enterprise Library的配置文件中。先来看一下该Facility的相关信息:
Facility Information
|
|
Uses Proxy
|
No
|
Requires Configuration
|
Yes
|
Uses Attributes
|
No
|
Version
|
Beta 2
|
二.使用Facility
1
.配置文件,这里使用配置文件注册组件的方式,放在应用程序配置文件中,这里唯一需要注意的是configurationkey,这个特性不能写错:
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="enterpriselibrary.configurationSettings" type ="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
< section name ="castle" type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</ configSections >
< enterpriselibrary .configurationSettings xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
defaultSection ="" applicationName ="Application" xmlns ="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration" >
< configurationSections >
< configurationSection name ="EditorSettings" encrypt ="false" >
< storageProvider xsi:type ="XmlFileStorageProviderData" name ="XML File Storage Provider" path ="../../EditorSettings.config" />
< dataTransformer xsi:type ="XmlSerializerTransformerData" name ="Xml Serializer Transformer" >
< includeTypes />
</ dataTransformer >
</ configurationSection >
</ configurationSections >
< keyAlgorithmStorageProvider xsi:nil ="true" />
</ enterpriselibrary.configurationSettings >
< castle >
< facilities >
< facility id ="configuration" type ="Castle.Facilities.EnterpriseLibrary.Configuration.EnterpriseConfigurationFacility, Castle.Facilities.EnterpriseLibrary.Configuration" />
</ facilities >
< components >
< component id ="editorfontdata" type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests"
configurationkey ="EditorSettings" />
< component id ="editorservice" type ="Castle.Facilities.EnterpriseLibrary.Configuration.Tests.EditorService, Castle.Facilities.EnterpriseLibrary.Configuration.Tests" />
</ components >
</ castle >
</ configuration >
< configuration >
< configSections >
< section name ="enterpriselibrary.configurationSettings" type ="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
< section name ="castle" type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</ configSections >
< enterpriselibrary .configurationSettings xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
defaultSection ="" applicationName ="Application" xmlns ="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration" >
< configurationSections >
< configurationSection name ="EditorSettings" encrypt ="false" >
< storageProvider xsi:type ="XmlFileStorageProviderData" name ="XML File Storage Provider" path ="../../EditorSettings.config" />
< dataTransformer xsi:type ="XmlSerializerTransformerData" name ="Xml Serializer Transformer" >
< includeTypes />
</ dataTransformer >
</ configurationSection >
</ configurationSections >
< keyAlgorithmStorageProvider xsi:nil ="true" />
</ enterpriselibrary.configurationSettings >
< castle >
< facilities >
< facility id ="configuration" type ="Castle.Facilities.EnterpriseLibrary.Configuration.EnterpriseConfigurationFacility, Castle.Facilities.EnterpriseLibrary.Configuration" />
</ facilities >
< components >
< component id ="editorfontdata" type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests"
configurationkey ="EditorSettings" />
< component id ="editorservice" type ="Castle.Facilities.EnterpriseLibrary.Configuration.Tests.EditorService, Castle.Facilities.EnterpriseLibrary.Configuration.Tests" />
</ components >
</ castle >
</ configuration >
2
.编写数据类文件:
public
class
EditorFontData
{
private string name;
private float size;
private int style;
public EditorFontData()
{
}
public string Name
{
get{ return name; }
set{ name = value; }
}
public float Size
{
get{ return size; }
set{ size = value; }
}
public int Style
{
get{ return style; }
set{ style = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Name = {0}; Size = {1}; Style = {2}", name, size.ToString(), style.ToString());
return sb.ToString();
}
}
{
private string name;
private float size;
private int style;
public EditorFontData()
{
}
public string Name
{
get{ return name; }
set{ name = value; }
}
public float Size
{
get{ return size; }
set{ size = value; }
}
public int Style
{
get{ return style; }
set{ style = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Name = {0}; Size = {1}; Style = {2}", name, size.ToString(), style.ToString());
return sb.ToString();
}
}
3
.采用XML方式的存储
<?
xml version="1.0" encoding="utf-8"
?>
< EditorSettings >
< xmlSerializerSection type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
< EditorFontData xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< Name > Microsoft Sans Serif </ Name >
< Size > 9.25 </ Size >
< Style > 0 </ Style >
</ EditorFontData >
</ xmlSerializerSection >
</ EditorSettings >
< EditorSettings >
< xmlSerializerSection type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
< EditorFontData xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< Name > Microsoft Sans Serif </ Name >
< Size > 9.25 </ Size >
< Style > 0 </ Style >
</ EditorFontData >
</ xmlSerializerSection >
</ EditorSettings >
4
.使用数据类的组件
public
class
EditorService
{
private readonly EditorFontData data;
public EditorService(EditorFontData data)
{
this.data = data;
}
public EditorFontData Data
{
get { return data; }
}
}
{
private readonly EditorFontData data;
public EditorService(EditorFontData data)
{
this.data = data;
}
public EditorFontData Data
{
get { return data; }
}
}
5
.在容器中使用数据类
[TestFixture]
public class FacilityTestCase
{
[Test]
public void LoadingConfig()
{
IWindsorContainer container = new WindsorContainer( new XmlInterpreter(new AppDomainConfigSource()) );
EditorService service = (EditorService) container[ typeof(EditorService) ];
Assert.AreEqual("Microsoft Sans Serif", service.Data.Name);
Assert.AreEqual(9.25,service.Data.Size);
}
}
public class FacilityTestCase
{
[Test]
public void LoadingConfig()
{
IWindsorContainer container = new WindsorContainer( new XmlInterpreter(new AppDomainConfigSource()) );
EditorService service = (EditorService) container[ typeof(EditorService) ];
Assert.AreEqual("Microsoft Sans Serif", service.Data.Name);
Assert.AreEqual(9.25,service.Data.Size);
}
}
可以看到,使用
EnterpriseLibrary Configuration Facility非常的简单。最后还要注意一点,使用这个
Facility需要安装
Enterprise Library,因为它依赖于:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Configuration.dll
Microsoft.Practices.EnterpriseLibrary.Configuration.dll
三.原理分析
下面对这个Facility的原理做一下简单的分析。在初始化的时候,它注册了一个名为
EntLibConfigurationInspector
的分发器
public
class
EnterpriseConfigurationFacility : AbstractFacility
{
protected override void Init()
{
Kernel.ComponentModelBuilder.AddContributor( new EntLibConfigurationInspector() );
}
}
internal class EntLibConfigurationInspector : IContributeComponentModelConstruction
{
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (model.Configuration == null) return;
String configKey = model.Configuration.Attributes["configurationkey"];
if (configKey == null) return;
model.ExtendedProperties["configurationkey"] = configKey;
model.CustomComponentActivator = typeof(EntLibComponentActivator);
}
}
{
protected override void Init()
{
Kernel.ComponentModelBuilder.AddContributor( new EntLibConfigurationInspector() );
}
}
internal class EntLibConfigurationInspector : IContributeComponentModelConstruction
{
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (model.Configuration == null) return;
String configKey = model.Configuration.Attributes["configurationkey"];
if (configKey == null) return;
model.ExtendedProperties["configurationkey"] = configKey;
model.CustomComponentActivator = typeof(EntLibComponentActivator);
}
}
在
EntLibConfigurationInspector中为
ComponentModel注册一个
CustomComponentActivator类型的
Activator,这个
CustomComponentActivator的实现为
EntLibComponentActivator。
internal
class
EntLibComponentActivator : AbstractComponentActivator
{
public EntLibComponentActivator(ComponentModel model, IKernel kernel,
ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction) : base(model, kernel, onCreation, onDestruction)
{
}
protected override object InternalCreate()
{
String configKey = (String) Model.ExtendedProperties["configurationkey"];
return ConfigurationManager.GetConfiguration(configKey);
}
protected override void InternalDestroy(object instance)
{
String configKey = (String) Model.ExtendedProperties["configurationkey"];
ConfigurationManager.WriteConfiguration(configKey, instance);
}
}
{
public EntLibComponentActivator(ComponentModel model, IKernel kernel,
ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction) : base(model, kernel, onCreation, onDestruction)
{
}
protected override object InternalCreate()
{
String configKey = (String) Model.ExtendedProperties["configurationkey"];
return ConfigurationManager.GetConfiguration(configKey);
}
protected override void InternalDestroy(object instance)
{
String configKey = (String) Model.ExtendedProperties["configurationkey"];
ConfigurationManager.WriteConfiguration(configKey, instance);
}
}
好了,关于 EnterpriseLibrary Configuration Facility就简单的介绍到这里。
更多Castle文章可以访问:《Castle 开发系列文章》
本文转自lihuijun51CTO博客,原文链接:
http://blog.51cto.com/terrylee/67691
,如需转载请自行联系原作者