自定義WebConfig

在系統開發過程中,經常遇到添加一下配置信息的需求,有時候需要在App.config或Web.config中添加,如果永遠都配置在appSetting節點下面,配置項多了,就顯得比較混亂,其實我們也可以自定義節點配置。


首先,在configSection節點中添加自定義的配置信息,如下,其中Type中用於解釋該配置信息的類與程序集名稱。如下:

	<configSections>
		<section name="dataSourceSettings" type="XXX.App.ServicePlanEmailPromotion.DataSourceSettings,XXX.App.ServicePlanEmailPromotion"/>
	</configSections>

然後就可以在WebConfig下添加自己定義的節點信息,跟定義xml文件格式一樣,可自由定義。如下:

<dataSourceSettings dataBase="TempTable">
		<tables>
			<add name="DM_ESPOR_SonicWall_Result_CS_B2B" type="CS" />
			<add name="DM_ESPOR_SonicWall_Result_EM_B2B" type="EM" />
			<add name="DM_ESPOR_SonicWall_Result_CS_B2C" type="CS"/>
			<add name="DM_ESPOR_SonicWall_Result_EM_B2C" type="EM"/>
		</tables>
</dataSourceSettings>

Webconfig中需要的節點配置信息定義好之後,就需要建立對應的Class來讀取這些信息,也就是上面提到的 XXX.App.ServicePlanEmailPromotion.DataSourceSettings.

創建的Class內容如下:

    public sealed class DataSourceSettings : ConfigurationSection
    {
        [ConfigurationProperty("tables", IsRequired = true)]
        public TableCollection Tables
        {
            get
            {
                return (TableCollection)base["tables"];
            }
        }

        [ConfigurationProperty("dataBase", IsRequired = true)]
        public string DataBaseName
        {
            get
            {
                return (string)base["dataBase"];
            }
        }
    }

其中Tables和DataBaseName兩個屬性分別對應配置文件中tables和dataBase的內容。

TableCollection是一個集合類,如下:

public sealed class TableCollection : ConfigurationElementCollection
    {
        private IList<DataSourceTable> tableList;

        protected override ConfigurationElement CreateNewElement()
        {
            return new TableElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            TableElement tableElement = element as TableElement;

            return tableElement.Name;
        }

        public IList<DataSourceTable> TableList
        {
            get
            {
                if (this.tableList == null)
                {
                    tableList = new List<DataSourceTable>();
                    foreach (TableElement table in this)
                    {
                        tableList.Add(new DataSourceTable { Name = table.Name, Type = table.Type });
                    }
                }

                return this.tableList;
            }
        }

        public new string this[string name]
        {
            get
            {
                string type = string.Empty;

                if (this.tableList.Where(table => table.Name == name).Any())
                {
                    return this.tableList.Where(table => table.Name == name).FirstOrDefault().Type;
                }
                else
                {
                    throw new ArgumentNullException("Not found " + name + " in tables nodes.");
                }
            }
        }
    }

    public sealed class TableElement : ConfigurationElement
    {
        /// <summary>
        /// Table type.
        /// </summary>
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return (string)base["type"]; }
            set { base["type"] = value; }
        }

        /// <summary>
        /// Table name.
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }
    }

    public class DataSourceTable
    {
        public string Name { get; set; }

        public string Type { get; set; }
    }

那麼該如何使用呢? 請看下方:

            DataSourceSettings dataSource = ConfigurationManager.GetSection("dataSourceSettings") as DataSourceSettings;
            var dataSourceList = dataSource.Tables.TableList;





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章