Microsoft Enterprise Library---解读Configuration之元数据
看了Configuration的配置存储提供者和转换器后,我就在想:为什么那两个工厂类是根据什么依据来提供实际的存储提供者和转换器的呢?看了Configuration带的事例程序后,明白了这些信息是存放在系统使用者的配置文件里的,比如App。Config ,我来看看它是如何配置的:
<configuration>
<configSections>
<section name="enterpriselibrary.configurationSettings" type="Microsoft.Practices.EnterpriseLibrary.Configuration.ConfigurationManagerSectionHandler, Microsoft.Practices.EnterpriseLibrary.Configuration" />
</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="../../../EditorSettings.config" />
<dataTransformer xsi:type="XmlSerializerTransformerData" name="Xml Serializer Transformer">
<includeTypes />
</dataTransformer>
</configurationSection>
</configurationSections>
<keyAlgorithmStorageProvider xsi:nil="true" />
</enterpriselibrary.configurationSettings>
</configuration>
看过以后,呵,这是个典型的要用IConfigurationSectionHandler解析的配置文件,不管他,先看看一个configurationSection,他提供了某个配置数据的名称:Name,一看就知道是关键字,然后是storageProvider的数据类型,名称,将要读数据的出处,dataTransformer的数据类型,名称。其他的可以先不管了,我们把这样的信息叫做元数据吧,那么这些元数据是怎样进入Configuration的呢? 来看看以下类图:
原来,Configuration通过元数据全部解析到ConfigurationSettings里面了。ConfigurationSettings有个ConfigurationSectionDataCollection集合,里面有定义的ConfigurationSectionData,每个ConfigurationSectionData里又有TransformerData和StorageProviderData。
看到这里,ConfigurationSettings原来是个元数据信息实体类。
那么ConfigurationSettings是被哪个类初始的呢,在Configuration里有个ConfigurationFile类,这个类的功能就是将配置文件导入,并分析配置文件,将ConfigurationSettings初始,下面是它的主要属性和方法:
private Hashtable results:用于存放ConfigurationSettings。
Public Load(String): 根据“enterpriselibrary.configurationSettings”关键字,将ConfigurationSettings导入,此时,results里的值仅仅是“enterpriselibrary.configurationSettings”这个名称。
Public GetConfig(string):根据根据“enterpriselibrary.configurationSettings”关键字将ConfigurationSettings类初始化。在这个方法里,ConfigurationFile取出ConfigurationManagerSectionHandler,将ConfigurationSettings绑定数据。按NET的N层的角度来看。configurationSettings类为实体类,而ConfigurationFile就是实体访问类了。