1

I'm building a solution that allows me to cache common requests in a Mongo database. I would like to make this as generic as possible, so therefore, I've created ConfigurationElement that contains the following properties:

  • name
  • provider
  • maxItems

When written in a configuration file, this would look like the following:

<add name="mongoCachingProvider" provider="GeniDev.ApplicationBlocks.Data.Caching.Mongo.Configuration.MongoCachingBackingStoreElement, GeniDev.ApplicationBlocks.Data.Caching.Mongo"
     maxItems="10" />

The .NET class for this configuration section looks like:

public class CachingBackingStoreElement : ConfigurationElement
{
    #region Properties

    // Provide some constants that are used by this class.
    private const string CachingBackingStoreNameProperty = "name";
    private const string CachingBackingStoreProviderProperty = "provider";
    private const string CachingBackingStoreMaxItemsProperty = "maxItems";

    /// <summary>
    ///     Gets or sets the name of the provider.
    /// </summary>
    [ConfigurationProperty(CachingBackingStoreNameProperty, IsRequired = true)]
    public string Name
    {
        get { return (string)this[CachingBackingStoreNameProperty]; }
        set { this[CachingBackingStoreNameProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the provider.
    /// </summary>
    [ConfigurationProperty(CachingBackingStoreProviderProperty, IsRequired = true)]
    [TypeConverter(typeof(TypeNameConverter))]
    public Type Provider
    {
        get { return (Type)this[CachingBackingStoreProviderProperty]; }
        set { this[CachingBackingStoreProviderProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the maximum items that should be stored in the cache.
    /// </summary>
    [ConfigurationProperty(CachingBackingStoreMaxItemsProperty, IsRequired = true)]
    public int Port
    {
        get { return (int)this[CachingBackingStoreMaxItemsProperty]; }
        set { this[CachingBackingStoreMaxItemsProperty] = value; }
    }

    #endregion
}

Now, I need to have a Mongo configuration section that requires additional properties such as dbServer, dbName, dbCacheBucket and maxSizeInMB.

Therefore, I've created a new ConfigurationElement that inherits from the above one CachingBackingStoreElement.

The code results in the following:

public class MongoCachingBackingStoreElement : CachingBackingStoreElement
{
    #region Properties

    // Provide some constants that are used by this class.
    private const string MongoCachingBackingStoreServerProperty = "dbServer";
    private const string MongoCachingBackingStoreProperty = "dbPort";
    private const string MongoCachingBackingStoreNameProperty = "dbName";
    private const string MongoCachingBackingStoreBucketNameProperty = "dbCacheBucket";
    private const string MongoCachingBackingStoreMaxSizeInMb = "maxSizeInMB";

    /// <summary>
    ///     Gets or sets the server.
    /// </summary>
    [ConfigurationProperty(MongoCachingBackingStoreServerProperty, IsRequired = true)]
    public string Server
    {
        get { return (string)this[MongoCachingBackingStoreServerProperty]; }
        set { this[MongoCachingBackingStoreServerProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the port.
    /// </summary>
    [ConfigurationProperty(MongoCachingBackingStoreProperty, IsRequired = true)]
    public int Port
    {
        get { return (int)this[MongoCachingBackingStoreProperty]; }
        set { this[MongoCachingBackingStoreProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the database name.
    /// </summary>
    [ConfigurationProperty(MongoCachingBackingStoreNameProperty, IsRequired = true)]
    public string DbName
    {
        get { return (string)this[MongoCachingBackingStoreNameProperty]; }
        set { this[MongoCachingBackingStoreNameProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the bucket name.
    /// </summary>
    [ConfigurationProperty(MongoCachingBackingStoreBucketNameProperty, IsRequired = true)]
    public string CacheBucketName
    {
        get { return (string)this[MongoCachingBackingStoreBucketNameProperty]; }
        set { this[MongoCachingBackingStoreBucketNameProperty] = value; }
    }

    /// <summary>
    ///     Gets or sets the maximum size that the cache should take (in MegaBytes).
    /// </summary>
    [ConfigurationProperty(MongoCachingBackingStoreMaxSizeInMb, IsRequired = true)]
    public int MaxSize
    {
        get { return (int)this[MongoCachingBackingStoreMaxSizeInMb]; }
        set { this[MongoCachingBackingStoreMaxSizeInMb] = value; }
    }

    #endregion
}

This allows me to write the following in a config file:

<add name="mongoCachingProvider" provider="GeniDev.ApplicationBlocks.Data.Caching.Mongo.Configuration.MongoCachingBackingStoreElement, GeniDev.ApplicationBlocks.Data.Caching.Mongo"
     maxItems="10" dbServer="127.0.0.1" dbPort="27017" dbName="AppServer-Cache" dbCacheBucket="ODataRequestsCache" maxSizeInMB="500" />

When I request the configuration section right now, I do get the following error:

System.Configuration.ConfigurationErrorsExceptionUnrecognized attribute 'maxSizeInMB'. Note that attribute names are case-sensitive.

Anyone has an idea on how this could be resolved?

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • I found my answer here: http://stackoverflow.com/questions/4902461/configurationelementcollection-with-a-number-of-configurationelements-of-differe – Complexity Nov 17 '15 at 14:35

0 Answers0