自定义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;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值