在學習了Duwamish和.Text的配置方法後,對兩者的配置持久化做一個比較。
以ApplicationConfiguration為例,Duwamish的設定檔中的<ApplicationConfiguration>...</ApplicationConfiguration>部分對應於Duwamish.SystemFramework.ApplicationConfiguration類,
<configSections>
<section name="ApplicationConfiguration" type="Duwamish7.SystemFramework.ApplicationConfiguration, Duwamish7.SystemFramework" />
...
</configSections>
...
<ApplicationConfiguration>
<add key="SystemFramework.Tracing.Enabled" value="False" />
<add key="SystemFramework.Tracing.TraceFile" value="C:\Program Files\Microsoft Visual Studio .NET 2003\Enterprise Samples\Duwamish 7.0 CS\.\DuwamishTrace.txt" />
<add key="SystemFramework.Tracing.TraceLevel" value="4" />
<add key="SystemFramework.Tracing.SwitchName" value="DuwamishTraceSwitch" />
<add key="SystemFramework.Tracing.SwitchDescription" value="Error and information tracing for Duwamish" />
<add key="SystemFramework.EventLog.Enabled" value="True" />
<add key="SystemFramework.EventLog.Machine" value="." />
<add key="SystemFramework.EventLog.SourceName" value="Duwamish7" />
<add key="SystemFramework.EventLog.LogLevel" value="1" />
</ApplicationConfiguration>
ApplicationConfiguration類實現了System.Configuration.IConfigurationSectionHandler介面,需要實現一個create()方法,該方法將在調用System.Configuration.GetConfig()方法時自動被調用。在ApplicationConfiguration中有大量的private變數和與這些變數相對應的靜態public屬性,但這些public屬性僅實現了get{},而不實現set{}。例如,TracingEnabled屬性的實現:
public static bool TracingEnabled
{
get
{
return tracingEnabled;
}
}
其中,tracingEnabled為ApplicationConfiguration的私人變數。
另外,在ApplicationConfiguration類中還實現了一個ReadSetting方法,共有四個重載函數,分別提供int,string, bool, TraceLevel四種不同配置類型的設定讀取功能。以下為讀取string類型的ReadSetting的實現
public static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
{
try
{
Object setting = settings[key];
return (setting == null) ? defaultValue : (String)setting;
}
catch
{
return defaultValue;
}
}
另一個重要方法是OnApplicationStart(),它是一個靜態方法,在Global.asa中的Application_Start()事件中被調用,用於在web網站被訪問前,將設定讀入System.Configuration.ConfigurationSettings中,而ReadSetting方法正是從ConfigurationSettings中讀出設定的。ReadSetting的參數settings是通過baseHandler.Create()方法獲得ConfigurationSettings的引用的,如下所示
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
在設定檔中,我發現一個有趣的現象,add項中的key值為一些不存在類和屬性
<add key="SystemFramework.Tracing.Enabled" value="False" />
在以上代碼中,SystemFramework.Tracing根本不存在,更不用說Enabled屬性了。實際上,這個key僅僅用於標識變數,如果要讀取這個設定就可以用ReadSetting(settings,"SystemFramework.Tracing.Enabled",defaultValue)完成,而從web頁中讀取這個設定則可以用SystemFramework.ApplicationConfiguration.TracingEnabled靜態屬性。
如果我們想使設定檔與上層的類相對應,可以考慮建立一個SystemFramework.Tracing類(見),並加入Enabled屬性,然後在Enabled屬性中調用SystemFramework.ApplicationConfiguration.TracingEnabled,這樣就可以根據類來對配置進行有效分類,同時,也保證配置結構與上層結構的一致
namespace SystemFramework
{
class Tracing
{
public static bool Enabled
{
get{return ApplicationConfiguration.TracingEnabled;}
}
}
}
最後是我畫的配置持久化模型圖:
參考:
《Duwamish深入剖析-配置篇》http://www.aspcool.com/lanmu/browse1.asp?ID=1045&bbsuser=aspnet