当前位置:主页   - 电脑 - 网站开发 - ASP.Net
Castle IOC容器实践之EnterpriseLibrary Configuration Facility
来源:网络   作者:   更新时间:2012-02-22
收藏此页】    【字号    】    【打印】    【关闭

  主要内容:

  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="http://tech.ddvip.com/http://tech.ddvip.com/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();
  }
}

  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>
4.使用数据类的组件

public class EditorService
{
  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);
  }
}

  可以看到,使用EnterpriseLibrary Configuration Facility非常的简单。最后还要注意一点,使用这个Facility需要安装Enterprise Library,因为它依赖于:

  Microsoft.Practices.EnterpriseLibrary.Common.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);
  }
}

  在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)
  {
c }
  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就简单的介绍到这里。

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明