2

I have set up a configuration system that will grab the configuration from a MySQL database. And I have got it working, but now I realize that to get the Value from the Cache, I have to use this long messy code.

CacheLayer.Instance.Caches["app_config"].Current.EmailALertInterval

I would like to remove Current, but I haven't managed to figure out if it's possible to have the class do this directly. At the moment the implementation looks like this.

public T Current
{
    get
    {
        if (_current == null)
        {
            ReloadConfiguration();
        }

        return _current;
    }
}

But I would like to simply it to this:

CacheLayer.Instance.Caches["app_config"].EmailALertInterval

I am looking at something like this, but this only works with indexers.

public T this[T key]

EDIT: Just to add some more context I will add some more code.

This is CacheLayer. It essentially allows me to store multiple configurations. I may have one that is for example the general app config, but I can also grab an array of Emails used.

public Dictionary<String,IGenericCache<dynamic>> Caches
{
    get
    {
        return _caches;
    }
}

public void AddCache(String config)
{
    _caches.Add(config,new GenericCache<dynamic>(config));
}

Inside my GenericCache I load the configuration using a JSON string stored in a MySQL db.

_current = JsonConvert.DeserializeObject<T>(db.dbFetchConfig(config));

The reason that GenericConfig is T and not dynamic is because I want to be able to make a custom implmentation outside of the CacheLayer, one that does not necessarily use dynamic.

ANother example on how I want this to be used.

List<String> EmailList = CacheLayer.Instance.Caches["EmailList"].Current;

This would in fact grab an JSON Array from the MySQL containing a list of Emails.

Any ideas?

Steve Konves
  • 2,648
  • 3
  • 25
  • 44
David
  • 417
  • 1
  • 4
  • 7

2 Answers2

2

Add a new property

public int EmailALertInterval
{
    get { return Current.EmailALertInterval; }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks. I did unfortunately forgot to mention in the original post that I use `dynamic` in most cases. So `EmailAlertInterval` is actually generated at runtime, not compile-time. – David Jun 10 '12 at 14:54
1

You have lots of those configurations and EmailAlertInterval is just one example, right?

Then you have to change the Caches class, as you already mentioned.

But, as you already know which Caches will go into the CacheLayer (as for as i understand your example), you could have properties there, e.g.

CacheLayer.Instance.Caches.AppConfig.EmailALertInterval

And that property handles what Current does now.

public T AppConfig
{
    get
    {
        if (appConfig == null)
        {
           return ReloadConfiguration();
        }

        return appConfig;
    }
}

think that should be more elegant

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • I like the idea, but at the mment I don't think that would work. I updated my post with some more example code on how CacheLayer is designed, as well as the GenericCache class. – David Jun 10 '12 at 15:04
  • If you like the idea, lets make it work ;) And you already know that there >should< be a app_config part, so i do not see a problem getting this to work. perhaps this will help you: http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c and of course that one http://www.codeproject.com/Articles/21107/Dynamic-Properties-Implementation-Using-C-Generics – Mare Infinitus Jun 10 '12 at 15:16
  • Awesome. Going to dig into those two. I think it might be what I am looking for. Yea, there should always be a app_config, but there might also be additional configurations, like email lists, various components etc. I load plugins and such and the idea is that they should all have their sepeare configurations. – David Jun 10 '12 at 15:20
  • if you know before which plugins can be loaded, it is quite the same. if there are dynamic parts that you do not know already, you still have the Dictionary to handle those. – Mare Infinitus Jun 10 '12 at 15:30