Enterprise Library: Configuration Application Block应用向导篇, Part 2

Enterprise Library: Configuration Application Block应用向导篇

Part 2

Written by: Rickie Lee (rickieleemail#yahoo.com)

My blog: www.cnblogs.com/rickie


Enterprise Library: Configuration Application Block应用向导篇, Part 1

Configuration Application Block应用向导

这里演示Configuration Application Block内建支持的XML File Storage ProviderXML Serializer Transformer,来读写XML配置数据。应用程序需要添加对Microsoft.Practices.EnterpriseLibrary.Configuration.dll引用,同时在代码添加如下语句:

using Microsoft.Practices.EnterpriseLibrary.Configuration;

 

Demo程序界面如下:
Enterprise_ConfigurationApplicationBlock_Demo.JPG

1)使用Configuration Console工具编写配置文件

如下图所示,添加Configuration Application Block,并同时添加Configuration Section,命名为ProxySettings,另外添加Configuration Application Block内置的XML File Storage ProviderXML Serializer Transformer,并设置Xml File Storage ProviderFileName属性。具体操作可以参考Enterprise Library: Configuration Application Block文档。

Enterprise_CAB_ConfigurationConsole.JPG

如下是App.config配置文件,该配置文件仅仅包含一些元数据。

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <configSections>

    <section name="enterpriselibrary.configurationSettings" type="Microsoft.Practices.EnterpriseLibrary.Configuration.ConfigurationManagerSectionHandler, Microsoft.Practices.EnterpriseLibrary.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

  </configSections>

  <enterpriselibrary.configurationSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" applicationName="Application" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration">

  <configurationSections>

    <configurationSection name="ProxySettings" encrypt="false">

      <storageProvider xsi:type="XmlFileStorageProviderData" name="XML File Storage Provider" path="ProxySettingsConfiguration.config" />

      <dataTransformer xsi:type="XmlSerializerTransformerData" name="Xml Serializer Transformer">

        <includeTypes />

      </dataTransformer>

    </configurationSection>

  </configurationSections>

  <keyAlgorithmStorageProvider xsi:nil="true" />

  <includeTypes />

</enterpriselibrary.configurationSettings>

</configuration>

 

2)定义配置类

对应每一个应用程序,你需要定义应用程序访问的配置数据内存表示,也就是定义表示配置信息的类。根据配置数据的类型,表示形式可以是简单的字符串或Storage Provider返回的DataSet类型。另外一种选择是,配置数据可以以应用程序定义的复杂对象形式表示。有多种方法读写配置数据,每一种方法将影响数据的内存表示。

一种方法是使用Configuration Application Block内置的XmlSerializerTransformerXmlFileStorageProvider来读写配置数据。

如下定义配置类,作为XML配置文件的内存表示:

       public class ProxySettingsData

       {

              #region Private Members

              private string proxyServer;

              private int port;

              #endregion

 

              #region Public Accessors

              public string ProxyServer

              {

                     get { return proxyServer;}

                     set { proxyServer = value;}

              }

              public int Port

              {

                     get { return port;}

                     set { port = value;}

              }

              #endregion

 

              public ProxySettingsData()

              {

                     //

                     // TODO: Add constructor logic here

                     //

              }

 

              public override string ToString()

              {

                     StringBuilder sb = new StringBuilder();

                     sb.AppendFormat("Proxy Server = {0}; Port = {1}", proxyServer, port.ToString());

 

                     return sb.ToString();

              }

       }

 

To be continued. - Part 3 待续.
***

Enterprise Library: Configuration Application Block应用向导篇, Part 3


***

作者:Rickie Lee (rickieleemail#yahoo.com)

本文参考Enterprise Library, Configuration Application Block文档。

 

References:

1. Enterprise Library, Configuration Application Block

2. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html

3. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html

posted @ 2005-02-09 15:43  Rickie  阅读(4170)  评论(2编辑  收藏  举报