C#建立自定義配置節

weixin_33896726發表於2016-10-21

在.Net應用程式中,我們經常看到VS為我們生成的專案工程中都會含有nfig或者nfig這樣的檔案。這個檔案就是我們所說的應用程式配置檔案。在這個檔案裡面記述著一些與我們的應用程式相關的資訊,如:資料庫連線,認證模式等。我們在程式中可以利用ConfigurationManager的ConnectionStrings屬性方便的獲取配置檔案中的資料庫連線字串資訊。

  可是有時候,我們需要對它進行一些擴充套件,加入一些自定義的元素,而不是僅僅使用預設的配置。例如,我們可能需要在程式啟動時,動態的載入某個類,並對其進行初始化,而這個類或者初始化資料是我們在程式設計的時候所不知道的。相信大家都碰到過這樣的問題,這裡就不做過多的解釋了。最好的辦法無非就是把這些可能會改變的東西寫進配置檔案裡面,到你能夠確定的時候,你只需要修改配置檔案就Ok了,而不是產品馬上上線的時候還用30秒鐘開啟VS去改程式碼。

  新增一些自定義的元素到配置檔案中是很容易的,只需要兩步就能搞定:

  1. 在<configSections>節點中註冊你所要定義的節點名稱及用於處理該節點的配置節處理程式。程式碼如下:

    <configSections>
        <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/>
    </configSections>

  

  2. 在適當的位置新增自定義的節點。程式碼如下:

    <configSections>
        <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/>
    </configSections>

    <dbFactory>
        <default factory="sql"></default>
        <factorys>
            <add name="sql"  assembly="Hello.Data"  class="SqlDbFactory" />
            <add name="oracle" assembly="Hello.Data" class="OracleDbFactory" />             
        </factorys>
    </dbFactory>

 

  自定義節點算是新增完了,可是我們怎麼在程式裡面獲取這些配置資訊呢?相信大家很多都是玩XML的高手,用System.Xml下的一些類寫個XML的解析類,把我們自定義的節點資訊解析出來,不就得到我們想要的東西了嗎?的確是這樣,大牛果然是大牛,小弟實在是佩服。可是如果用這種方式來實現的話,小弟就實在沒有必要寫這篇博文了。

  .Net框架為我們實現了很多的配置API,來簡化我們對配置檔案的操作。在第一個步驟中,我們提到了配置節處理程式,這個程式就是用來讀寫我們自定義的節點資訊的。那麼我們又如何來實現一個配置節處理程式呢?首先我們來了解一下相關的類和概念。

  ConfigurationSection:自定義節點都要繼承該類,以提供對自定義配置節的自定義處理和程式設計訪問。

  ConfigurationElement:它表示配置檔案內的一個元素。

  ConfigurationElementCollection:它表示包含一個子元素集合的配置元素。

  有了這些類,我們可以歸納起來配置檔案中有兩種型別的配置元素。

  第一,單一型配置元素,即繼承於ConfigurationElement的元素,它不包含任何子元素。

  第二,集合型配置元素,即繼承於ConfigurationElementCollection德元素,它包含一個子元素集合。

  概念往往都比較抽象,要搞清楚這些東西,我們還是結合我們上面給的例子來具體說明一下。

  在<dbFactory> 配置節中有兩個元素,即<default>和<factorys>,而<factorys>元素又包含了兩個子元素。那麼在這裡<default>就是單一型配置元素,而<factorys>就是集合型配置元素。我們需要分別實現與這些元素相對應的類及其屬性。

  <default>元素:它是一個單一型的元素,所以我們繼承ConfigurationElement。該元素中有一個factory屬性,那麼我們在類中進行相應的定義。程式碼如下:

  程式碼

  

    public class DefaultElement : ConfigurationElement
    {
        [ConfigurationProperty("factory")]
        public string Factory
        {
            get
            {
                return this["factory"] as string;
            }
            set
            {
                this["factory"] = value;
            }
        }
    }

 

  注意:在屬性定義上面我們需要註冊該屬性的ConfigurationProperty特性。

  <factorys>子元素:

  程式碼

  

public class FactoryElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("assembly")]
        public string Assembly
        {
            get
            {
                return this["assembly"] as string;
            }
            set
            {
                this["assembly"] = value;
            }
        }

        [ConfigurationProperty("class")]
        public string Class
        {
            get
            {
                return this["class"] as string;
            }
            set
            {
                this["class"] = value;
            }
        }
    }

 

  <factorys>元素是集合型元素,繼承ConfigurationElementCollection。

  程式碼

  

public class FactoryElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FactoryElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FactoryElement)element).Name;
        }

        public FactoryElement this[string name]
        {
            get
            {
                return BaseGet(name) as FactoryElement;
            }
        }
    }

 

  ConfigurationElementCollection類是個抽象類,你應該顯示的實現它的CreateNewElement方法和GetElementKey方法。

  <dbFactory>節點,繼承於ConfigurationSection

  程式碼

  

public class DbFactorySection : ConfigurationSection
    {
        [ConfigurationProperty("default")]
        public DefaultElement DefaultFactory
        {
            get
            {
                return this["default"] as DefaultElement;
            }
            set
            {
                this["default"] = value;
            }
        }

        [ConfigurationProperty("factorys")]
        public FactoryElements Factorys
        {
            get
            {
                return this["factorys"] as FactoryElements;
            }

            set
            {
                this["factorys"] = value;
            }
        }
    }

 

  配置節處理程式終於寫完了,把這四個類放在同一個工程目錄下,編譯成一個DLL。在你需要獲取配置資訊的地方,引用這個DLL,用DbFactorySection section = ConfigurationManager.GetSection( "dbFactory" ) as DbFactorySection;試試,section是不是你想要的東西呢?

相關文章