几个开源项目配置信息的存储和处理的方式

最近在看duwamish7,asp.net forums, dottext几个优秀的开源(微软官方)的项目
因我目前所处的技术水平的阶段的原因,我看这些项目程序,更加关注的是具体的实现
次之才是架构

比较第一篇:几个开源项目实体层实现方式比较

这次的关注点是它们存储和处理配置信息的不同方式

一,duwamish7和asp.net forums
这两者处理方式有相同之处,都是通过实现IConfigurationSectionHandler来实现配置类
配置类的代码如下:

    public class DuwamishConfiguration : IConfigurationSectionHandler
    
{
        
private static string
 dbConnectionString  ;
        
private static bool
 enablePageCache ;

        
IConfigurationSectionHandler 成员


        
public static string ConnectionString
        
{
            
get return dbConnectionString ; }

        }


        
public static bool EnablePageCache
        
{
            
get return enablePageCache ; }

        }

    }

web.config如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<configSections>
     
<section name="DuwamishConfiguration" type="Duwamish7.Common.DuwamishConfiguration, Duwamish7.Common"  />
  
</configSections>
  
<DuwamishConfiguration>
     
<add key="dbConnectionString" value="server=localhost;User ID=Duwamish7;Password=password;database=Duwamish7;Connection Reset=FALSE" />
     
<add key="enablePageCache" value="true" />
  
</DuwamishConfiguration>
  
<system.web> 
  
<compilation debug="true" />

 
</system.web>
</configuration>

然后就可以DuwamishConfiguration.ConnectionString获得数据库连接,DuwamishConfiguration.xxxx获得你定义的其他
数据了,不过这样用之前,需要先调用这个方法哦
System.Configuration.ConfigurationSettings.GetConfig("DuwamishConfiguration") ;
通常这个方法会放在Global.asa的application_start事件处理中,或者自己定义的
httpmodule的application_start类似事件中

关于duwamish7配置信息处理的更多信息,可以参考:
Duwamish深入剖析-配置篇
由Duwamish学习web.config的配置

二,dottext配置信息的存储和处理
dottext配置信息不是放在web.config,而是放在一个自己定义的blog.config文件中:

<?xml version="1.0" encoding="utf-8" ?> 
<BlogConfigurationSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  
<ConnectionString>Data Source=KWK;Database=blog;UID=sa;Pwd=sa;</ConnectionString>
  
<EnablePageCache>true</EnablePageCache>
</BlogConfigurationSettings>
然后通过串行化的方式获取数据,先定义对应的类:
[Serializable]
public class
 BlogConfigurationSettings
{
   
private string
 _connectionString ;
   
public string
 ConnectionString
   
{
       
get return _connectionString ; }

       
set { _connectionString = value ; }
   }

   
   
private bool _enablePageCache ;
   
public bool
 EnablePageCache
   
{
       
get return _enablePageCache ; }

       
set { _enablePageCache = value ; }
   }

}


然后可以通过如下方法获得这些配置类对象:
                public static BlogConfigurationSettings Instance(HttpContext context)
        
{
                      
//在实际的应用中,别忘了加上缓存

              string filepath = context.Server.MapPath("~/blog.config");
              settings 
= (BlogConfigurationSettings)LoadSerializedObject(typeof
(BookConfigurationSettings),filepath);
              
return
 settings;
        }

        
public static object LoadSerializedObject(Type type, string filename)
        
{
            FileStream fs 
= null
;
            
try

            
{
                
// open the stream

                fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
                XmlSerializer serializer 
= new
 XmlSerializer(type);
                
return
 serializer.Deserialize(fs);
            }

            
catch(Exception e)
            
{
                
throw
 e;
            }

            
finally
            
{
                
if(fs != null
)
                    fs.Close();
            }

        }

大家都知道一般ASP.NET Application 的配置信息都写在一个叫 WEB.Config 的文件中。这个文件是一个标准的XML 文档。通过文档中的元素.net平台会知道该应用程序所要建立的环境。当然也会有我们自己写入的信息。以下的内容就是通过实例说明一下Web.Congif 文件的结构与动态配置。

    所有信息都在<configuration>与</configuration>之间,它就是XML文档的根标签.在这个标签中有两个主要的信息区域:1.配置块驱动描述区域.2.配置块设定区域

    配置块驱动描述区域在<configSections>和</configSections>标签之间.它包含了几个<section>标签,标签中描述了配置块名称与处理该配置块的类库名称.如:

 
<configuration>
   <configSections>
      <section name="appSettings"
         type="System.Configuration.NameValueFileSectionHandler,
         System, Version=1.0.3300.0,
         Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <section name="sessionState"
         type="System.Web.SessionState.SessionStateSectionHandler,
         System.Web, Version=1.0.3300.0, Culture=neutral,
         PublicKeyToken=b03f5f7f11d50a3a"
         allowDefinition="MachineToApplication"/>
</configSections>

<appSettings>
   < /span><add key="dsn"value="localhost;uid=MyUserName;pwd=;"/>
   <add key="msmqserver" value="server\myqueue"/>
</appSettings>

<sessionState cookieless="true" timeout="10"/>
</configuration>

    那我们怎么在程序中取得这些设定的信息呢?当然我们要建立自己的配置块驱动类.这个类必须实现一个接口:IConfigurationSectionHandler ,有了这个接口我们就可以从配置块中读取数据了.

下面是IConfigurationSectionHandler 在BCL 中的定义:

namespace System.Web.Configuration
{
    public interface IConfigurationSectionHandler
    {
        public Object Create(Object parent, Object input, XmlNode node);
    }
}

在BCL中已经有几个类实现了该接口:

 
类名 说明
DictionarySectionHandler 读取配置节的键/值对配置信息。
IgnoreSectionHandler 为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
NameValueSectionHandler 提供配置节中的名称/值对配置信息。
SingleTagSectionHandler 提供一种将配置节中的 XML 属性读取为键/值对的方法。

有了以上信息,我们可以来读取appSetting 中的信息了

 
NameValueCollection config = (NameValueCollection)
ConfigurationSettings.GetConfig("appSetting");
Response.Write("The value of key_one is " + config["dsn"] + "<br>");
Response.Write("The value of key_two is " + config["msmqserver"] + "<br>");