Reputation: 417
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?
Upvotes: 2
Views: 380
Reputation: 8192
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
Upvotes: 1
Reputation: 112782
Add a new property
public int EmailALertInterval
{
get { return Current.EmailALertInterval; }
}
Upvotes: 2