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 Provider和XML Serializer Transformer,来读写XML配置数据。应用程序需要添加对Microsoft.Practices.EnterpriseLibrary.Configuration.dll引用,同时在代码添加如下语句:
using Microsoft.Practices.EnterpriseLibrary.Configuration;
Demo程序界面如下:
(1)使用Configuration Console工具编写配置文件
如下图所示,添加Configuration Application Block,并同时添加Configuration Section,命名为ProxySettings,另外添加Configuration Application Block内置的XML File Storage Provider和XML Serializer Transformer,并设置Xml File Storage Provider的FileName属性。具体操作可以参考Enterprise Library: Configuration Application Block文档。
如下是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=
</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/
<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内置的XmlSerializerTransformer和XmlFileStorageProvider来读写配置数据。
如下定义配置类,作为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
{
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.
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