Reputation: 365
Is it possible in .net to do something like implement overridable/situational config properties?
Example: Say I have a URL in my system config, but the URL may be different based on certain contexts or callers. Silver/Gold/Platinum as an example. The same config would have something like this: Url_Standard: "www.standard.com" Url_Gold: "www.gold.com" etc
I would like to have a strongly typed config so that I can simply program against AppSettings.Url as opposed to AppSettings.Url_Standard, AppSettings.Url_Gold, etc.
The way I have handled this, for now, is to have a configurationHelper with methods that look like GetUrl(string entityContext) and, within there, I figure out what value to return. Problem is this creates a ConfigHelper class with a method for almost every property in the config. Ugly, SRP problems, OCP problems. I'd much prefer a strongly-typed config class that behaves the same way and gets rid of the magic strings. Being able to just call AppSettings.Url (and know it's returning me the most specific one). I don't see any way around a parameter or a generic since the individual call conveys the context. This can't just be loaded/calculated at startup.
Design ideas?
Upvotes: 1
Views: 140
Reputation: 38094
In my view, you can use Dicitonary<TKey, TValue>
to store handlers of your config keys. So it will be just one place which should be edited when new key will be added. And it will be comply with Single Responsibility Principle. Moreover, other classes wille be comply with Open Closed principle.
Let me show an example via C#. This is our abstrction of config handler:
public interface IConfigHandler
{
void Handle(string value);
}
And is concrete implementation:
public class UrlHandler : IConfigHandler
{
public void Handle(string value)
{
// your logic here to handle "standard", "gold", "etc"
}
}
And this is a place where you will add new config handlers. It is like simple factory:
public class ConfigHandlerFactory
{
private Dictionary<string, IConfigHandler> _handlerByKeyOfConfig =
new Dictionary<string, IConfigHandler>()
{
{ "url", new UrlHandler() }
};
public IConfigHandler GetInstance(string key) =>
_handlerByKeyOfConfig[key];
}
So our factory has just one handler UrlHandler
. Here UrlHandler
is a strategy to handle config keys. So you can add new strategy into this factory ConfigHandlerFactory
. So, here we've applied strategy pattern.
And then you can call your code like this:
ConfigHandlerFactory configHandlerFactory = new ConfigHandlerFactory();
string urlKey = "url"; // you can create a class with keys to avoid magic strings
IConfigHandler configHandler = configHandlerFactory.GetInstance(urlKey);
configHandler.Handle("www.gold.com");
Upvotes: 1