My program consists from several "services" (I'm not talking about SOA, so don't get confused), and each of these "services" is using some configuration (for example, a directory path). This configuration should be read from a configuration file, as they tend to change (without changing the program's logic, that is).
So my question is this - Should each "service" receive its configuration when invoked, or should it be responsible for getting its own configuration?
This question is actually very similar to this question, only mine is more specific about these:
Thanks.
I would advocate a combination of these approaches, as long as you manage to implement it in an elegant way (I have seen this done in both elegant and horrendous implementations):
Have a data structure for configuration that can implement settings sub-sets (in Perl it is extremely simple with super-imposed hashes, in other languages it's a bit more wordy but overlaying maps on top of each other works as well). To try to be more precise, your data structures will be
A global Singleton Configuraton map
A "passed as parameter" local configuration map
Have a Configuration loader class, which can:
Take "passed as parameter" local configuration map
Take a set of keys needed by the component
Superimpose the local map onto global configuration map
Allow the components to query out the values from combined map.
This way, each caller of the services has a complete freedom to either live in a worry-free word and let each service take care of its own configuration needs, or for a more controllable behavior, has an option of passing specific configuration settings it wants to control to the service (either creating the settings values by hand, or cloning data subset from global configuration and adjusting it).
This has major testing benefit, since the test framework can pass specific configuration local overrides needed for each test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With