首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 自定義配置節完整開發指南:ConfigurationSection 實戰與最佳實踐

     編輯:關於.NET

兩步走:聲明 + 實現

1. 配置文件聲明

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

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

2. 元素類實現

單一元素:DefaultElement

public class DefaultElement : ConfigurationElement
{
    [ConfigurationProperty("factory", IsRequired = true)]
    public string Factory
    {
        get => (string)this["factory"];
        set => this["factory"] = value;
    }
}

集合元素項:FactoryElement

public class FactoryElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get => (string)this["name"];
        set => this["name"] = value;
    }

    [ConfigurationProperty("assembly", IsRequired = true)]
    public string Assembly
    {
        get => (string)this["assembly"];
        set => this["assembly"] = value;
    }

    [ConfigurationProperty("class", IsRequired = true)]
    public string Class
    {
        get => (string)this["class"];
        set => this["class"] = value;
    }
}

集合元素:FactoryElements

public class FactoryElements : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
        => new FactoryElement();

    protected override object GetElementKey(ConfigurationElement element)
        => ((FactoryElement)element).Name;

    public FactoryElement this[string name]
        => BaseGet(name) as FactoryElement;
}

配置節:DbFactorySection

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

    [ConfigurationProperty("factorys")]
    public FactoryElements Factorys
    {
        get => (FactoryElements)this["factorys"];
        set => this["factorys"] = value;
    }
}

3. 程式碼讀取

var section = ConfigurationManager.GetSection("dbFactory") as DbFactorySection;
if (section != null)
{
    string defaultFactory = section.DefaultFactory.Factory;
    foreach (FactoryElement fe in section.Factorys)
    {
        Console.WriteLine($"{fe.Name}: {fe.Assembly}, {fe.Class}");
    }
}

關鍵類繼承體系

ConfigurationElement           // 單一元素基類
  → ConfigurationElementCollection // 集合元素基類 (抽象,需實現 CreateNewElement/GetElementKey)
ConfigurationSection           // 配置節基類 (對應 XML 根節點)
ConfigurationManager.GetSection // 統一讀取入口

進階技巧

  • 驗證:重寫 PostDeserialize() 執行跨屬性驗證
  • 預設值:[ConfigurationProperty("name", DefaultValue="sql")]
  • 加密:configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
  • 外部配置文件:<section configSource="dbFactory.config" />
  • 運行時修改:section.SectionInformation.ForceSave = true; config.Save()

總結

.NET 配置系統提供完整的「聲明式 XML + 強類型物件」映射機制。掌握 ConfigurationElement/Collection/Section 三層體系,即可構建類型安全、IntelliSense 友好、支援驗證加密的企業級配置模型,遠優於手工 XML 解析。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved