操作自定义配置节
操作自定义配置节
摘要:在网站设计上,经常使用Web.Config文件解耦合。例如通过配置文件确定实例化哪个工厂,得到不同的数据访问层。这就需要一个灵活的操作配置文件的机制。鄙人在学习过程中,将知识记下。
相关知识点:
1. ConfigurationPropertyAttribute属性类
指示 .NET Framework 如何实例化自定义配置元素属性的类型
您可以使用 ConfigurationPropertyAttribute 修饰配置属性,此配置属性将会指示 .NET Framework 使用修饰参数的值对该属性进行实例化和初始化。
创建自定义配置元素最简便的方法就是使用属性化(声明性)模型。您可以声明自定义公共属性 (Property),并使用 ConfigurationPropertyAttribute 属性 (Attribute) 来修饰它们。对于每一个标记有此属性 (Attribute) 的属性 (Property),.NET Framework 都使用反射来读取修饰参数,并创建相关的 ConfigurationProperty 实例。也可以使用编程模型,在这种情况下,您将负责声明自定义的公共属性,并返回它们的集合。
详细查看:
ConfigurationPropertyAttribute 类
开始设计:
1. 添加自定配置块
向 ASP.NET 配置文件添加自定义节处理程序
1. 将
注意: |
将 section 元素嵌套在 sectionGroup 中是可选的,但是建议这样做,以便更好地组织配置数据。 |
2. 可以在另一个配置文件中添加节处理程序声明,该配置文件不必是添加自定义配置元素的配置文件,只要声明节处理程序的配置文件在配置文件的层次结构中位于较高的位置。有关更多信息,请参见 ASP.NET 配置文件层次结构和继承。
3. section 元素的 type 属性必须与程序集清单匹配,否则将出现配置错误。程序集文件必须与定义它的 Web.config 文件位于相同的 ASP.NET 应用程序目录。
| 复制代码 |
<configuration> <!-- Configuration section-handler declaration area. --> <configSections> <section name="MyCustomGroup" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" type="Model.MyCustomGroupSection,Model"/> </configSections> <!-- Configuration section settings area. --> </configuration> |
4. 在 Web.config 文件的配置节设置区域中添加自定义配置元素。
| 复制代码 |
<configuration> <!-- Configuration section-handler declaration area. --> <!-- Configuration section settings area. --> <MyCustomGroup> <CustomCollection> <Customer Name="gecko" Age="18"/> <Customer Name="geckos" Age="18"/> </CustomCollection> </MyCustomGroup> <!-- Other configuration settings, like <system.web> --> </configuration> |
2. 创建自定义配置节处理程序
创建一个继承
| 复制代码 |
namespace Model { [Serializable] public class MyCustomGroupSection : ConfigurationSection { private const string MyCustomGroupSectionName = "MyCustomGroup"; private const string MyCunstiomCollection = "CustomCollection"; [ConfigurationProperty(MyCunstiomCollection, IsDefaultCollection = true)] public CustomCollection ListMyCustomCollection { get { return (base[MyCunstiomCollection] as CustomCollection); } } public static MyCustomGroupSection value { get { return (ConfigurationManager.GetSection(MyCustomGroupSectionName) as MyCustomGroupSection); } } } } |
添加您自己的代码,以执行所需的配置工作。下面的代码从自定义节中获取值。
创建一个继承至System.Configuration..::.ConfigurationElement类的公共类. 如下面的代码示例所示。
这个类映射了Customer节
| 复制代码 |
using System; using System.Configuration; namespace Model { [Serializable] public class Customer : ConfigurationElement { //Fields private const string NameProperty = "Name"; private const string AgeProperty = "Age"; //Properties [ConfigurationProperty(NameProperty,IsRequired=true)] public string Name { get { return (string)base[NameProperty]; } set { base[NameProperty] = value; } } [ConfigurationProperty(AgeProperty,IsKey=false)] public int Age { get { return (int)base[AgeProperty]; } set { base[AgeProperty] = value; } } } } |
创建一个继承至System.Configuration..::.ConfigurationElementCollection类的公共类. 如下面的代码示例所示。
这个类映射了CustomCollection节集合.
| 复制代码 |
using System.Configuration; namespace Model { public class CustomCollection : ConfigurationElementCollection { //Methods protected override ConfigurationElement CreateNewElement() { return new Customer(); } protected override object GetElementKey(ConfigurationElement element) { Customer entity = element as Customer; return entity.Name; } // Properties public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } protected override string ElementName { get { return "Customer"; } } public Customer this[string name] { get { return (base.BaseGet(name) as Customer); } } public Customer this[int index] { get { return (base.BaseGet(index) as Customer); } } } } |
至此,我们便可以用
Model.MyCustomGroupSection.value.ListMyCustomCollection[name].properties
的方式操作配置节内容了.