I’m looking for a best practice for managing configuration on a project with multiple class libraries. I’m looking for maintainability and ease of implementation.
Let’s assume a simple example: A console project with 2 class libraries. Each class library need their own configuration settings, and there are some settings that are common to several.
Class Library 1
- CL1Setting
- GlobalSetting
Class Library 2
- CL2Setting
- GlobalSetting
A first approach would be to create all the necessary settings on the main project:
- GlobalSetting
- CL1Setting
- CL2Setting
But this present several problems:
- It can get cluttered fast if there are lots of settings.
- It is not easy to maintain: How to know which settings are needed for each library?
- It can create naming conflicts. What if CL1Setting and CL2Setting would have the same name?
An ideal solution for me (although I’m afraid not possible) would be having custom library settings in separate files, or at least different sections. Something like this:
<configuration>
<appSettings>
<add key="globalSetting" value="cl1Global"/>
</appSettings>
<appSettings file="CL1.config" >
<add key="cl1setting" value="cl1setting1"/>
</appSettings>
<appSettings file="CL2.config">
<add key="cl2setting" value="cl2setting2"/>
</appSettings>
</configuration>
Any suggestions?
EDIT
As Ken Henderson suggests, config sections are another approach. However, although with their own advantages, they require coding, so I don’t find it ideal though. (This will probably end up being the best option though)
EDIT 2
joseph.ferris suggestion to look at Configuration Section Designer on CodePlex (csd.codeplex.com) was good. I found further problems, reported here (in case some is interested) http://csd.codeplex.com/discussions/278354
I think what you are looking for is a custom configuration section instead of appSettings. This is commonly used by 3rd party libraries (log4net is the first that comes to mind) to provide a way to configure their settings via your app/web config file. Note that this also provides the basis for how MS creates their configuration sections.
I’ve successfully used this in several different projects including one that included the ability to add new implementations of algorithms to an analysis program.