摘要: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,这个特性不能写错:
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场<?xml version="1.0" encoding="utf-8" ?>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
<configuration>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
<configSections>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<section name="enterpriselibrary.configurationSettings" type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
</configSections>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
<enterpriselibrary.configurationSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        defaultSection
="" applicationName="Application" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration">
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<configurationSections>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场            
<configurationSection name="EditorSettings" encrypt="false">
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场                
<storageProvider xsi:type="XmlFileStorageProviderData" name="XML File Storage Provider" path="../../EditorSettings.config" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场                
<dataTransformer xsi:type="XmlSerializerTransformerData" name="Xml Serializer Transformer">
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场                    
<includeTypes />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场                
</dataTransformer>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场            
</configurationSection>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
</configurationSections>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<keyAlgorithmStorageProvider xsi:nil="true" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
</enterpriselibrary.configurationSettings>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
<castle>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<facilities>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场            
<facility id="configuration" type="Castle.Facilities.EnterpriseLibrary.Configuration.EnterpriseConfigurationFacility, Castle.Facilities.EnterpriseLibrary.Configuration" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
</facilities>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
<components>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场            
<component id="editorfontdata" type="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests"
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场                configurationkey
="EditorSettings" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场            
<component id="editorservice" type="Castle.Facilities.EnterpriseLibrary.Configuration.Tests.EditorService, Castle.Facilities.EnterpriseLibrary.Configuration.Tests" />
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场        
</components>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
</castle>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
</configuration>
2.编写数据类文件:
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场public class EditorFontData
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_59
{       
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
private string  name;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
private float   size;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
private int     style;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public EditorFontData()
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_69    
{          
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public string Name 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_76    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_79        
getreturn name; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_83        
set{ name = value; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }
 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public float Size 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_89    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_92        
getreturn size; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_96        
set{ size = value; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }
 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public int Style 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_102    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_105        
getreturn style; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_109        
set{ style = value; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }
 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public override string ToString() 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_115    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        StringBuilder sb 
= new StringBuilder();
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        sb.AppendFormat(
"Name = {0}; Size = {1}; Style = {2}", name, size.ToString(), style.ToString());
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        
return sb.ToString();
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}
3.采用XML方式的存储
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场<?xml version="1.0" encoding="utf-8"?>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
<EditorSettings>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场  
<xmlSerializerSection type="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
<EditorFontData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场      
<Name>Microsoft Sans Serif</Name>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场      
<Size>9.25</Size>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场      
<Style>0</Style>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场    
</EditorFontData>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场  
</xmlSerializerSection>
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
</EditorSettings>
4.使用数据类的组件
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场public class EditorService
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_145
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
private readonly EditorFontData data;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public EditorService(EditorFontData data)
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_151    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        
this.data = data;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public EditorFontData Data
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_158    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_161        
get return data; }
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}
5.在容器中使用数据类
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场[TestFixture]
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
public class FacilityTestCase
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_168
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    [Test]
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public void LoadingConfig()
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_173    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        IWindsorContainer container 
= new WindsorContainer( new XmlInterpreter(new AppDomainConfigSource()) );
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        EditorService service 
= (EditorService) container[ typeof(EditorService) ];
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        Assert.AreEqual(
"Microsoft Sans Serif", service.Data.Name);
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        Assert.AreEqual(
9.25,service.Data.Size);
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}
可以看到,使用EnterpriseLibrary Configuration Facility非常的简单。最后还要注意一点,使用这个Facility需要安装Enterprise Library,因为它依赖于:
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场Microsoft.Practices.EnterpriseLibrary.Common.dll
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场Microsoft.Practices.EnterpriseLibrary.Configuration.dll
三.原理分析
下面对这个Facility的原理做一下简单的分析。在初始化的时候,它注册了一个名为EntLibConfigurationInspector的分发器
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场public class EnterpriseConfigurationFacility : AbstractFacility
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_191
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
protected override void Init()
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_195    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62Kernel.ComponentModelBuilder.AddContributor( 
new EntLibConfigurationInspector() );
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场
internal class EntLibConfigurationInspector : IContributeComponentModelConstruction
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_204
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public void ProcessModel(IKernel kernel, ComponentModel model)
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_208    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        
if (model.Configuration == nullreturn;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        String configKey 
= model.Configuration.Attributes["configurationkey"];
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        
if (configKey == nullreturn;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        model.ExtendedProperties[
"configurationkey"= configKey;
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        model.CustomComponentActivator 
= typeof(EntLibComponentActivator);
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}
EntLibConfigurationInspector中为ComponentModel注册一个CustomComponentActivator类型的Activator,这个CustomComponentActivator的实现为EntLibComponentActivator
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场internal class EntLibComponentActivator : AbstractComponentActivator
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_223
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
public EntLibComponentActivator(ComponentModel model, IKernel kernel, 
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction) : 
base(model, kernel, onCreation, onDestruction)
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_229    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
protected override object InternalCreate()
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_实践_236    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        String configKey 
= (String) Model.ExtendedProperties["configurationkey"];
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        
return ConfigurationManager.GetConfiguration(configKey);
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62    
protected override void InternalDestroy(object instance)
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_休闲_246    
{
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        String configKey 
= (String) Model.ExtendedProperties["configurationkey"];
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62        ConfigurationManager.WriteConfiguration(configKey, instance);
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_62
Castle IOC容器实践之EnterpriseLibrary Configuration Facility_IOC_73    }

Castle IOC容器实践之EnterpriseLibrary Configuration Facility_职场_124}

好了,关于EnterpriseLibrary Configuration Facility就简单的介绍到这里。
更多Castle文章可以访问:《Castle 开发系列文章