Custom Configuration section

I HATE “ConfigurationSettings.AppSettings” !!!!

That being said, .NET gives us a great alternative that no one talks about, custom configuration sections. It gives us a fully nestable and strongly typed config section.

Add a new class:

using System;
using System.Configuration;

namespace ISI.HAPP.API
{
  public sealed partial class Configuration : ConfigurationSection
  {
    public class AuthenicationParameters : ConfigurationElement
    {
      #region UseNTPermissions
      /// <summary>
      /// Gets or sets the UseNTPermissions.
      /// </summary>
      [ConfigurationProperty("UseNTPermissions", IsRequired = true)]
      public bool UseNTPermissions
      {
        get
        {
          return (bool)base["UseNTPermissions"];
        }
        set
        {
          base["UseNTPermissions"] = value;
        }
      }
      #endregion

      #region DefaultNTDomain
      /// <summary>
      /// Gets or sets the DefaultNTDomain.
      /// </summary>
      [ConfigurationProperty("DefaultNTDomain", IsRequired = false)]
      public string DefaultNTDomain
      {
        get
        {
          return (string)base["DefaultNTDomain"];
        }
        set
        {
          base["DefaultNTDomain"] = value;
        }
      }
      #endregion
    }

    #region Current
    public static readonly Configuration Current;
    static Configuration()
    {
      const string ConfigurationSectionName = "ISI.HAPP.API.Configuration";

      Current = null;
      if (Current == null) Current = (Configuration)(System.Configuration.ConfigurationManager.GetSection(ConfigurationSectionName));
      if (Current == null) throw new Exception(string.Format("Missing {0} Configuration Section", ConfigurationSectionName));
    }
    #endregion

    public Configuration() : base() { }

    #region CompanyName
    [ConfigurationProperty("CompanyName", IsRequired = false)]     public string CompanyName
    {
      get
      {
        return (string)base["CompanyName"];
      }
      set
      {
        base["CompanyName"] = value;
      }
    }
    #endregion

    #region Authenication
    public AuthenicationParameters Authenication
    {
      get
      {
        return (AuthenicationParameters)base["Authenication"];
      }
    }
    #endregion
  }
}

and add the following to the “configSections” of your app/web.config:

    <section name="ISI.HAPP.API.Configuration" type="ISI.HAPP.API.Configuration, ISI.HAPP.API.Core" requirePermission="false"/>

and the following after the “configSections”

  <ISI.HAPP.API.Configuration>
    <CompanyName>ISI</CompanyName>
    <Authenication>
      <UseNTPermissions>true</UseNTPermissions>
      <DefaultNTDomain>ISI</DefaultNTDomain>
    </Authenication>
  </ISI.HAPP.API.Configuration>

 

It’s that simple …….

Published 06/19/2009 20:19:50 (UTC) by Ron Muth

kick it on DotNetKicks.com  Shout it  vote it on WebDevVote.com

Comments

DotNetShoutout Custom Configuration section

10/24/2009 12:04:57 by DotNetShoutout
Thank you for submitting this cool story - Trackback from DotNetShoutout

Leave a Comment

(required)
(required)
(optional, never published)
(optional)
(required)