logo
  • Jobs
  • About Me
  • Contact
  • Home
« No more stored procedures…YAY!
Recruiting Advice »

Custom Configuration Sections in Beta 2

Posted June 10th, 2005 by Matt Berther

I spent a little time this afternoon delving into Whidbey Beta 2, after finally surfacing from other priorities at work.

One of the things that I’ve been really interested in is the Provider pattern, and so I decided to start understanding a little more how this pattern works in Whidbey. I’ve used this pattern quite a bit in .NET 1.1, however, the nice thing about 2.0 is that a lot of the base classes are already there for you.

As I was investigating further, there was one thing that really caught my eye. This was the new Configuration mechanism which you can use to create your own custom configuration sections. You know have the option to do this all declaratively (by deriving ConfigurationSection), instead of manually parsing the XML like you did with 1.1.

As I was looking through some of the pre-built ConfigurationSections, in my mind, there was one that was conspicuously missing considering the emphasis of the Provider pattern in .NET 2.0 and that was a Provider section.

What I hoped for was a class that would allow me to declare a section that looked something like this:

<sectionName defaultProvider="">
    <providers>
        <add name=""
            type="" />
    </providers>
</sectionName>

Unfortunately, I did not find one, so I wrote one out. This also helped me to understand the new configuration mechanism, and saves me from having to re-implement the same logic for any other providers I may author in the future.

Take this code:

public class ProviderSection : ConfigurationSection
{
    private readonly ConfigurationProperty defaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), null);
    private readonly ConfigurationProperty providers = new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null);
    private ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
 
    public ProviderSection()
    {
        properties.Add(providers);
        properties.Add(defaultProvider);
    }
 
    [StringValidator(MinLength = 1)]
    [ConfigurationProperty("defaultProvider")]
    public string DefaultProvider
    {
        get { return (string)base[defaultProvider]; }
        set { base[defaultProvider] = value; }
    }
 
    [ConfigurationProperty("providers")]
    public ProviderSettingsCollection Providers
    {
        get { return (ProviderSettingsCollection)base[providers]; }
    }
 
    protected override ConfigurationPropertyCollection Properties
    {
        get { return properties; }
    }
}

Immediately, you should notice the two attributes on the DefaultProvider property. The ConfigurationProperty attribute denotes which attribute should be used for loading the property, and the StringValidator attribute insures that the attribute is at least one character long. How cool is this? To write something like this in 1.1 would have taken easily 3 times as much code.

No Comments

This entry was posted on Friday, June 10th, 2005 at 3:54 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010