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 :
- <CouchbaseViews>
- <Entities>
- <Entity type=“UserEntitlement“>
- <Index name=“email“designDoc=“dev_Entitlement“view=“by_email“/>
- <Index name=“Contact“designDoc=“dev_Entitlement“view=“by_contactId“/>
- </Entity>
- <Entity type=“Entitlement“>
- <Index name=“Entitlement“designDoc=“dev_Entitlement“view=“by_entitlementId“/>
- </Entity>
- </Entities>
- </CouchbaseViews>
Code Snippet
- // First, I will create a CouchbaseViewsSection derived class to act
- // as the root of the configuration hierarchy and as the
- // config handler on the application config file.
- public class CouchbaseViewsSection : ConfigurationSection
- {
- [ConfigurationProperty(“Entities”, IsDefaultCollection = false)]
- public Entities Entities
- {
- get
- {
- Entities entitiesConfigElement = (Entities)base[“Entities”];
- return entitiesConfigElement;
- }
- }
- }
- //Now to allow more sub levels what we need to do is derive the Entities class
- //from ConfigurationElementCollection instead of ConfigurationElement and to
- //populate the AddElementName property with the name of the XML
- //element on my configuration file, in my case Entity.
- public class Entities : ConfigurationElementCollection
- {
- public Entities()
- {
- AddElementName = “Entity”;
- }
- protected override ConfigurationElement CreateNewElement()
- {
- return new Entity();
- }
- protected override Object GetElementKey(ConfigurationElement element)
- {
- return ((Entity)element).Type;
- }
- }
- //To support another level on the hierarchy, I derive the class returned
- //by the CreateNewElement() method (Entity) from the ConfigurationElementCollection.
- //This also doesn’t prevent me from adding properties to the class that will to be represented
- //as attributes in the XML file.
- public class Entity : ConfigurationElementCollection
- {
- public Entity()
- {
- AddElementName = “Index”;
- }
- [ConfigurationProperty(“type”, IsRequired = true, IsKey = true)]
- public string Type
- {
- get
- {
- return (string)this[“type”];
- }
- set
- {
- this[“type”] = value;
- }
- }
- protected override ConfigurationElement CreateNewElement()
- {
- return new Index();
- }
- protected override Object GetElementKey(ConfigurationElement element)
- {
- return ((Index)element).Name;
- }
- }
- //Now again i populate the AddElementName with the name of the
- //child elements on the XML file and return last element of the
- //hierarchy (Index), which derives directly from ConfigurationElement.
- public class Index : ConfigurationElement
- {
- [ConfigurationProperty(“name”, IsRequired = true, IsKey = true)]
- public string Name
- {
- get
- {
- return (string)this[“name”];
- }
- set
- {
- this[“name”] = value;
- }
- }
- [ConfigurationProperty(“designDoc”, IsRequired = true)]
- public string DesignDoc
- {
- get
- {
- return (string)this[“designDoc”];
- }
- set
- {
- this[“designDoc”] = value;
- }
- }
- [ConfigurationProperty(“view”, IsRequired = true)]
- public string View
- {
- get
- {
- return (string)this[“view”];
- }
- set
- {
- this[“view”] = value;
- }
- }
Now lets retrieve values from my config and store in a dictionary .
- public static Dictionary<string, List<EntityIndex>> CouchbaseViews;
- // This method LoadCouchbaseViews() gets all the value
- // from configuration and add it to a Dictionary.
- private static void LoadCouchbaseViews()
- {
- if (ConfigurationManager.GetSection(“CouchbaseViews”) != null)
- {
- CouchbaseViewsSection couchbaseViews =
- (CouchbaseViewsSection)System.Configuration.ConfigurationManager.GetSection(
- “CouchbaseViews”);
- CouchbaseViews = new Dictionary<string, List<EntityIndex>>();
- foreach (Entity entity in couchbaseViews.Entities)
- {
- List<EntityIndex> entityIndexes = new List<EntityIndex>();
- foreach (Index index in entity)
- {
- EntityIndex entityIndex = new EntityIndex();
- entityIndex.Name = index.Name;
- entityIndex.DesignDoc = index.DesignDoc;
- entityIndex.View = index.View;
- entityIndexes.Add(entityIndex);
- }
- CouchbaseViews.Add(entity.Type.ToString(), entityIndexes);
- }
- }
- }
And After all the work we get a Dictionary with the values .