I'm working on a system where we consistently apply "patterns" or approaches such as Dependency Injection.
I'd rather not expose the current underlying technology as not to get biased solutions, because I think my question would apply to many different languages and runtimes. It suffices to say that I'm using an Object Oriented platform, where I have classes, delegates, and all shenanigans expected on a modern language space.
Throughout development, I followed the following pattern:
I have a class called
ConfigurationProviderwhich has a simple methodgetwhere you can pass himsectionNames, by which one can retrieve configuration in a very decoupled way. The configuration returned by get is actually a hash structure, form which the caller can then pass configuration keys and retrieve configuration values.Most high-level
servicesthat I have expect to receive a instance of this object in order to access configuration. This way, they are able to configure themselves. This give me a certain flexibility to move configuration around (the final structure provided to each object must, of course, be respected) without affecting the caller object.
Let's give an example:
ConfigurationProvider config = new ConfigurationProvider();
Cache cache = new Cache(config);
UserProfileService userProfileSvc = new UserProfileService(cache);
As you can see, all cache details such as host and port, default time-to-live, etc., are completely abstracted. Users must then follow the rules of cache configuration. For instance, my application could have a configuration file called config.json:
{
"cache": {
"host": "localhost",
"port": 5555,
"logErrors": true
}
}
However, a problem arises (or should I call it trade-off?). The object is now coupled to the ConfigurationProvider. That can be largely solved by reimplementing its interface, so that you could pass any configuration you'd like as an object, but it's a lot of hassle. Of course, I can (and certainly should) create a constructor overload that receives the configuration directly instead of the provider.
While it may seems that I've answered any question I might have, my question still is the, is there a pattern, or at least convention, on this matter? Is this an anti-pattern?