OT: Configuration

OT: Configuration

Old forum URL: forums.lhotka.net/forums/t/5908.aspx


ajj3085 posted on Monday, December 01, 2008

Hi,

I've been addressing a need I have for configuration for some time by having my own custom code that redirects to different sections in the configuration file.  What I want is basically multiple addSetting sections, and a way to switch between them with a simple change to the configuration file.  So I'll have something like this:

    <configSections>
        <section name="UnitTest" type="MyCompany.Configuration.MedSettingsSectionHandler,MyCore" />
        <section name="devel" type="MyCompany.Configuration.MedSettingsSectionHandler,MyCore" />
    </configSections>
   
    <appSettings>
        <add key="Environment" value="UnitTest" />
    </appSettings>

    <UnitTest>
         <!-- unit test settings here -->
    </UnitTest>
    <devel>
         <!-- local machine development settings here -->
    </devel>

The Configuration Application Block in EntLib was replaced by System.Configuration in .Net 2... but I haven't figured out how to get similar functionality with it.

Any ideas?
Thanks
andy

SonOfPirate replied on Monday, December 01, 2008

Andy,

There are plenty of articles out there that should get you started.  But, in a nutshell, you should find it very easy to switch from the App Block to Sys.Config.  To start with, you'll need to create a new class that inherits from System.Configuration.ConfigurationSection, as follows:

public sealed class MySection : System.Configuration.ConfigurationSection
{
    private static System.Configuration.ConfigurationProperty _myProperty;
 
    public MySection() : base()
    {
        _myProperty = new System.Configuration.ConfigurationProperty(
                          "myProperty",
                          typeof(MyPropertySettings),
                          null,
                          System.Configuration.ConfigurationPropertyOptions.None);
 
        Properties.Add(_myProperty);
    }
 
    [System.Configuration.ConfigurationProperty("enabled", DefaultValue = "false")]
    public System.Boolean Enabled
    {
        get { return (System.Boolean)base["enabled"]; }
        set { base["enabled"] = value; }
    }
 
    [System.Configuration.ConfigurationProperty("myProperty")]
    public MyPropertySettings MyProperty
    {
        get { return base["myProperty"] as MyPropertySettings; }
        set { base["myProperty"] = value; }
    }
}

This is a simple example of a section with a single attribute and one child element.  It would appear in the config file as:

<configSections>
    <section name="mySection" type="MySection" />
</configSections>

<mySection enabled="[true|false]">
    <myProperty ...>
</mySection>

To create the inner element, you'd follow the same pattern except inherit from ConfigurationElement.

Hope that gets you started in the right direction!

 

ajj3085 replied on Monday, December 01, 2008

Pirate, thanks, that is something I'll look into, although I was hoping what I wanted was more baked in already.  My goal in posting this was to be able to get rid of more custom code I've written, much like I got rid of my custom DAL in favor of L2S.  Smile [:)]

Copyright (c) Marimer LLC