程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Castle IOC容器實踐之EnterpriseLibrary Configuration Facility

Castle IOC容器實踐之EnterpriseLibrary Configuration Facility

編輯:關於.NET

主要內容:

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="" xmlns:xsi=""
    defaultSection="" applicationName="Application" xmlns="">
    <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();
  }
}

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="" xmlns:xsi="">
   <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)
  {
  }
  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. 下一頁:
Copyright © 程式師世界 All Rights Reserved