Sunil Singh

Asp.net Creating a Custom Configuration Section that Contains a Collection of Collections

Examples I found on different websites on how to create custom configuration were not as good as i needed , I had to store the views of couchbase in my configuration file so that I can easily retrieve it . More about couchbase view here .

What I will do here is try to make it simple by creating a console app and walk you through my code . Lets start with the configuration.
The custom multi level configuration which i will use :

  1. <CouchbaseViews>
  2.     <Entities>
  3.         <Entity type=UserEntitlement>
  4.             <Index name=emaildesignDoc=dev_Entitlementview=by_email/>
  5.             <Index name=ContactdesignDoc=dev_Entitlementview=by_contactId/>
  6.         </Entity>
  7.         <Entity type=Entitlement>
  8.             <Index name=EntitlementdesignDoc=dev_Entitlementview=by_entitlementId/>
  9.         </Entity>
  10.     </Entities>
  11. </CouchbaseViews>

Code Snippet
  1.     // First, I will create a CouchbaseViewsSection derived class to act
  2.     // as the root of the configuration hierarchy and as the
  3.     // config handler on the application config file.
  4.     public class CouchbaseViewsSection : ConfigurationSection
  5.     {
  6.         [ConfigurationProperty(“Entities”, IsDefaultCollection = false)]
  7.         public Entities Entities
  8.         {
  9.             get
  10.             {
  11.                 Entities entitiesConfigElement = (Entities)base[“Entities”];
  12.                 return entitiesConfigElement;
  13.             }
  14.         }
  15.     }
  16.     //Now to allow more sub levels what we need to do is derive the Entities class
  17.     //from ConfigurationElementCollection instead of ConfigurationElement and to
  18.     //populate the AddElementName property with the name of the XML
  19.     //element on my configuration file, in my case Entity.
  20.     public class Entities : ConfigurationElementCollection
  21.     {
  22.         public Entities()
  23.         {
  24.             AddElementName = “Entity”;
  25.         }
  26.         protected override ConfigurationElement CreateNewElement()
  27.         {
  28.             return new Entity();
  29.         }
  30.         protected override Object GetElementKey(ConfigurationElement element)
  31.         {
  32.             return ((Entity)element).Type;
  33.         }
  34.     }
  35.     //To support another level on the hierarchy, I derive the class returned
  36.     //by the CreateNewElement() method (Entity) from the ConfigurationElementCollection.
  37.     //This also doesn’t prevent me from adding properties to the class that will to be represented
  38.     //as attributes in the XML file.
  39.     public class Entity : ConfigurationElementCollection
  40.     {
  41.         public Entity()
  42.         {
  43.             AddElementName = “Index”;
  44.         }
  45.         [ConfigurationProperty(“type”, IsRequired = true, IsKey = true)]
  46.         public string Type
  47.         {
  48.             get
  49.             {
  50.                 return (string)this[“type”];
  51.             }
  52.             set
  53.             {
  54.                 this[“type”] = value;
  55.             }
  56.         }
  57.         protected override ConfigurationElement CreateNewElement()
  58.         {
  59.             return new Index();
  60.         }
  61.         protected override Object GetElementKey(ConfigurationElement element)
  62.         {
  63.             return ((Index)element).Name;
  64.         }
  65.     }
  66.     //Now again i populate the AddElementName with the name of the
  67.     //child elements on the XML file and return last element of the
  68.     //hierarchy (Index), which derives directly from ConfigurationElement.
  69.     public class Index : ConfigurationElement
  70.     {
  71.         [ConfigurationProperty(“name”, IsRequired = true, IsKey = true)]
  72.         public string Name
  73.         {
  74.             get
  75.             {
  76.                 return (string)this[“name”];
  77.             }
  78.             set
  79.             {
  80.                 this[“name”] = value;
  81.             }
  82.         }
  83.         [ConfigurationProperty(“designDoc”, IsRequired = true)]
  84.         public string DesignDoc
  85.         {
  86.             get
  87.             {
  88.                 return (string)this[“designDoc”];
  89.             }
  90.             set
  91.             {
  92.                 this[“designDoc”] = value;
  93.             }
  94.         }
  95.         [ConfigurationProperty(“view”, IsRequired = true)]
  96.         public string View
  97.         {
  98.             get
  99.             {
  100.                 return (string)this[“view”];
  101.             }
  102.             set
  103.             {
  104.                 this[“view”] = value;
  105.             }
  106.         }

 

Now lets retrieve values from my config and store in a dictionary .

  1.    public static Dictionary<string, List<EntityIndex>> CouchbaseViews;
  2.     // This method LoadCouchbaseViews() gets all the value
  3.     // from configuration and add it to a Dictionary.
  4.     private static void LoadCouchbaseViews()
  5.     {
  6.         if (ConfigurationManager.GetSection(“CouchbaseViews”) != null)
  7.         {
  8.             CouchbaseViewsSection couchbaseViews =
  9.               (CouchbaseViewsSection)System.Configuration.ConfigurationManager.GetSection(
  10.               “CouchbaseViews”);
  11.             CouchbaseViews = new Dictionary<string, List<EntityIndex>>();
  12.             foreach (Entity entity in couchbaseViews.Entities)
  13.             {
  14.                 List<EntityIndex> entityIndexes = new List<EntityIndex>();
  15.                 foreach (Index index in entity)
  16.                 {
  17.                     EntityIndex entityIndex = new EntityIndex();
  18.                     entityIndex.Name = index.Name;
  19.                     entityIndex.DesignDoc = index.DesignDoc;
  20.                     entityIndex.View = index.View;
  21.                     entityIndexes.Add(entityIndex);
  22.                 }
  23.                 CouchbaseViews.Add(entity.Type.ToString(), entityIndexes);
  24.             }
  25.         }
  26.     }

 
And After all the work we get a Dictionary with the values .