代码之家  ›  专栏  ›  技术社区  ›  Alfero Chingono

自定义web.config节处理程序

  •  3
  • Alfero Chingono  · 技术社区  · 15 年前

    我以前设计过一个自定义的节处理程序,但是我遇到了一个似乎无法思考的问题。我有一个这样的配置部分:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
        <configSections>
            <section name="providers" requirePermission="false" type="MyProject.Configuration.ProvidersSection"/>
        </configSections>
    
        <providers>
            <provider name="products">
                <queries>
                    <add name="insert" value="InsertProduct" />
                    <add name="retrieve" value="GetProduct" />
                    <add name="collect" value="GetProducts" />
                    <add name="update" value="UpdateProduct" />
                    <add name="delete" value="DeleteProduct" />
                <queries>
            </provider>
            <provider name="tasks">
                <queries>
                    <add name="insert" value="InsertTask" />
                    <add name="retrieve" value="GetTaskById" />
                    <add name="collect" value="GetMultipleTasks" />
                    <add name="update" value="UpdateTask" />
                    <add name="delete" value="DeleteTask" />
                <queries>
            </provider>
            <provider name="events">
                <queries>
                    <add name="insert" value="AddEvent" />
                    <add name="retrieve" value="GetEvent" />
                    <add name="collect" value="GetEvents" />
                    <add name="update" value="UpdateEvent" />
                    <add name="delete" value="DeleteEvent" />
                <queries>
            </provider>
        </providers>
    </configuration>
    

    我创建了以下处理程序类:

    using System.Configuration;
    
    namespace MyProject.Configuration
    {
        public class ProvidersSection : ConfigurationSection
        {
            public new Element this[string key]
            {
                get
                {
    
                }
            }
        }
    
        [ConfigurationCollection(typeof(ProviderElement))]
        public class ProvidersCollection : ConfigurationElementCollection
        {
    
            protected override ConfigurationElement CreateNewElement()
            {
                return new ProviderElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return element.ElementInformation.Properties["name"].Value;
            }
    
            public ProviderElement this[string key]
            {
                get
                {
                    return (ProviderElement)base.BaseGet(key);
                }
            }
        }
    
        public class ProviderElement : ConfigurationElement
        {
            public string this[string name]
            {
                get
                {
                    return string.Empty;
                }
            }
        }
    }
    

    为了成功执行以下代码,这些类中需要什么代码?

    string query = ProvidersSection["tasks"].Queries["Insert"];
    
    1 回复  |  直到 15 年前
        1
  •  4
  •   AJ.    15 年前

    你应该研究使用 ConfigurationElementCollection KeyValueConfigurationCollection 用于要用作集合的元素。在这种情况下,您必须创建一个元素集合,每个元素都有一个keyValueConfigurationCollection。因此,与上面的XML配置不同,您将拥有类似的配置:

    <providers>
        <provider key="products">
            <queries>
                 <add key="whatever" value="stuff" />
                 ...etc...
            <queries>
        <provider>
    </providers>
    

    您可以为每个“provider”重新使用“queries”元素,它将是您的keyValueConfigurationCollection。

    谷歌的快速搜索 this article on MSDN 也可能有帮助。

    编辑-代码示例

    根节定义如下:

    public class ProviderConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("Providers",IsRequired = true)]
        public ProviderElementCollection Providers
        {
            get{ return (ProviderElementCollection)this["Providers"]; }
            set{ this["Providers"] = value; }
        }
    }
    

    然后,您的提供者元素集合:

    public class ProviderCollection : ConfigurationElementCollection
    {
        public ProviderElement this[object elementKey]
        {
            get { return BaseGet(elementKey); }
        }
    
        public void Add(ProviderElement provider)
        {
            base.BaseAdd(provider);
        }
    
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new ProviderElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProviderElement)element).Key;
        }
    
        protected override string ElementName
        {
            get { return "Provider"; }
        }
    }
    

    然后,您的provider元素:

    public class Provider : ConfigurationElement
    {
        [ConfigurationProperty("Key",IsRequired = true, IsKey = true)]
        public string Key
        {
            get { return (string) this["Key"]; }
            set { this["Key"] = value; }
        }
    
        [ConfigurationProperty("Queries", IsRequired = true)]
        public KeyValueConfigurationCollection Queries
        {
            get { return (KeyValueConfigurationCollection)this["Queries"]; }
            set { this["Queries"] = value; }
        }
    }
    

    您可能需要对keyValueConfigurationCollection进行一些处理,以使其正常工作,但我认为这是一般的想法。然后,在代码中访问这些内容时,您将执行如下操作:

    var myConfig = (ProviderConfiguration)ConfigurationManager.GetSection("Providers");
    //and to access a single value from, say, your products collection...
    var myValue = myConfig.Providers["Products"].Queries["KeyForKeyValuePairing"].Value;
    

    希望有帮助。现在不要让我把它翻译成vb:-d