Enterprise Library Step By Step系列(二) 配置應用程式塊

來源:互聯網
上載者:User

一.響應配置變更通知:

Configuration Application Block提供了一個事件機制,當儲存的配置變更時通知應用程式 ,使用步驟:

1)建立一個EverntHandler

1/**//// <summary>
2    /// 建立EventHanler
3    /// </summary>
4    /// <param name="sender"></param>
5    /// <param name="args"></param>
6    private void OnConfigurationChanged(object sender, ConfigurationChangedEventArgs args)
7    {
8      Cursor = System.Windows.Forms.Cursors.WaitCursor;
9
10      EditorFontData configData = ConfigurationManager.GetConfiguration("EditorSettings") as EditorFontData;
11
12      StringBuilder results = new StringBuilder();
13      results.Append("Configuration changes in storage were detected. Updating configuration.");
14      results.Append(Environment.NewLine);
15      results.Append("New configuration settings:");
16      results.Append(Environment.NewLine);
17      results.Append('\t');
18      results.Append(configData.ToString());
19      results.Append(Environment.NewLine);
20
21      Cursor = System.Windows.Forms.Cursors.Arrow;
22    }

2)註冊事件

1/**////註冊事件
2    ConfigurationManager.ConfigurationChanged += new ConfigurationChangedEventHandler(OnConfigurationChanged);

二.配置資料的緩衝:

Configuration Application Block在設計時提供了對配置資料的緩衝,在讀取XML資料後,再次讀取它首先會判斷緩衝是否為空白,如果不為空白,它會直接從緩衝中讀取資料(在剖析篇中會有詳細的介紹)。

顯式的清除掉緩衝用下面這句代碼即可:

1/**////清除快取資料
2     ConfigurationManager.ClearSingletonSectionCache();

三.面向進階人員的擴充機制:

1. 除了用XML檔案可以儲存資料外,還可以建立自己的儲存方式,像SQL Server Database,註冊表格儲存體等,這時就需要我們自己建立StorageProvider。建立自訂的Storage Provider,需要注意以下幾點:

1)要讀取和寫入資料,需要繼承於StorageProvider類和分別實現IStorageProviderReader和IstorageProviderWriter介面:

1public class XmlFileStorageProvider : StorageProvider, IStorageProviderWriter
2    {
3      //……
4    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.