Tuesday, May 29, 2007

User-based Config Files

Here's another useful tip that was prompted after reading this post regarding location-specific config files. In a multi-developer environment it is sometimes necessary to have developer-specific parameters in your config files while maintaining the base configuration file, specifically when using source control. For example, developers may need different connection strings to access different databases during development and testing. In our case the base config file (app.config or web.config) contain a single connection string that is used by the DAL. Therefore, two different developers cannot have separate connection strings in the base config file.

To solve this problem we used the "file" attribute of the section of the config file to specify a user-specific set of configuration parameters. An example of the base config file:

The user.config file contains:

The contents of the section will be merged with the section of the base config file with the settings from user.config superceding those in the app/web.config. Any additional settings in the base config file are preserved, allowing us to have static config parameters in the base config file and only user-defineable parameters in the user config files. Finally, we set the user.config files to be excluded from source control.