I am already working on a .net project but I think this question can apply to any programming language/framework.
The question is what are the good practices for reading configuration for a class library. I can think of the following options:
- Read from the application domain’s config file. For example in case of .net web app, read from web.config
- Read from specific custom file made for this class lib. Like create a xml file for your class lib and put your configuration there.
- The caller should pass the configuration needed to class lib (thru class constructor). So in this case class lib does not directly read from a configuration file but instead expects the caller to send the required information.
Any ideas/preferences?
Surprisingly (or unsurprisingly?) it depends.
From a developer’s point of view, having the caller supply configuration via library API is preferable because it allows the library to be used in a wider variety of contexts, not dependent on how the library is being hosted. Providing a configuration loader that can pull from app domain or a private file is gravy, and reasonably usable defaults if no configuration is performed are a sanity-saver.
From an operator’s point of view, it is also useful to be able to override whatever settings a developer supplied because they don’t make sense in your environment. It’s not clear to me how this might work if the developer is deliberately trying to force the shape of the environment on you, though.
I think the best compromise is to allow for all three possibilities, as log4net does.