首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Enterprise Library 實戰系列(十五):配置應用程式區塊——設計篇

     編輯:關於.NET

概述

配置應用程式區塊為應用系統提供通用的配置管理解決方案,支援從多種儲存來源(XML、資料庫、登錄等)讀寫配置。其設計目標在於:

  • 提供簡單的讀/寫配置資料介面
  • 實現配置資料存取與實體儲存分離
  • 透過 Storage Provider 與 Transformer 在應用程式與實體儲存間傳遞資料
  • 採用抽象工廠模式建立 Provider

核心概念

Storage Provider
負責讀寫特定實體儲存的物件(如 XML 檔案、SQL 資料庫)
Transformer
在儲存格式與應用程式格式之間轉換配置資料的物件

架構設計

類別關係圖

配置區塊將配置中繼資料與實際配置設定分離:中繼資料存放於應用程式組態檔(App.config/Web.config),實際設定可存放於獨立檔案。中繼資料包含儲存位置、Transformer 與 Storage Provider 型別等資訊。

中繼資料與配置區段關係

核心類別職責

  • ConfigurationManager:靜態外觀類別,提供 GetConfiguration/WriteConfiguration 簡單 API(外觀模式)
  • ConfigurationBuilder:建立 Storage Provider 與 Transformer,管理快取
  • IStorageProviderReader/Writer:定義讀/寫介面,內建 XmlFileStorageProvider
  • ITransformer:轉換介面,內建 XmlSerializerTransformer

快取機制

配置區段設定快取於雜湊表。讀取時優先檢查快取;Storage Provider 偵測到儲存層變更時自動清除快取。亦可透過 ConfigurationManager.ClearSingletonSectionCache() 手動清除。

擴展性:自訂 Storage Provider

Provider 擴展架構

開發者可依實體儲存類型實作自訂 Provider,透過組態工具切換無需修改程式碼,體現「組態驅動」設計哲學。

簡化的程式碼存取

// 讀取
MyConfigClass configData = ConfigurationManager.GetConfiguration("MySettings") as MyConfigClass;

// 寫入
ConfigurationManager.WriteConfiguration("MySettings", configData);

ReadConfiguration / WriteConfiguration 核心流程

讀取流程

public object ReadConfiguration(string sectionName)
{
    ValidateSection(sectionName);
    object configurationSection = sections.GetSection(sectionName);
    if (IsConfigurationSectionCached(configurationSection)) return configurationSection;

    IStorageProviderReader storageProviderReader = CreateStorageProvider(sectionName);
    object configurationSettings = storageProviderReader.Read();
    if (configurationSettings == null) return null;

    ITransformer transformer = CreateTransformer(sectionName);
    if (transformer != null)
        configurationSection = transformer.Deserialize(configurationSettings);
    else
        configurationSection = configurationSettings;

    sections.AddSection(sectionName, configurationSection, 
        new ConfigurationChangedEventHandler(OnExternalConfigurationChanged), 
        storageProviderReader);
    return configurationSection;
}

寫入流程

public void WriteConfiguration(string sectionName, object configValue)
{
    ValidateSection(sectionName);
    ConfigurationChangingEventArgs args = CreateConfigurationChangingEventArgs(sectionName, configValue);
    OnConfigurationChanging(args);
    if (!args.Cancel)
    {
        IStorageProviderWriter configStorageWriter = GetConfigurationStorageWriter(sectionName);
        object writeData = GetSerializedDataToWrite(sectionName, configValue);
        ConfigurationWriterActionCommand writerActionCommand = new ConfigurationWriterActionCommand(configStorageWriter, writeData);

        if (!sections.ContainsSection(sectionName))
            AddSection(sectionName, configValue, configStorageWriter);
        else
            sections.UpdateSection(sectionName, writerActionCommand, configValue);

        OnConfigurationChanged(new ConfigurationChangedEventArgs(configFile.FileName, sectionName));
    }
}

Provider 與 Factory 階層

Provider 階層 Factory 階層

  • IConfigurationProvider:所有 Provider 必須實作,含 Initialize() 與 ConfigurationName
  • ConfigurationProvider:抽象基類
  • ConfigurationFactory:抽象工廠基類
  • ProviderFactory:實作 IConfigurationProvider 並繼承 ConfigurationFactory

結語

引用 MSDN:「設計了配置應用程式區塊,您就可以用最適合應用程式需求的方式儲存配置資料,使您不受儲存方法的限制。」

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved