Developer Settings

Don't you just hate it when someone on the dev team changes the connection string or some other parameter in the application / web config and checks it in! In the team I am currently working with we end up having most config files excluded to avoid overwriting each others changes / differences.

Some of the reasons this problem happens, for us at least, are subtle differences in our local development environments. Some of us run full SQL Server, others use SQL express, some only have a C drive, others have SSD's and need a second drive to store other parts of the application. It ends up with various combinations of settings that differ for most people.

Excluding files does mitigate this problem to some extend but sometimes changes are required and they check their version of the file in. Then everyone who has a different configuration spends some time getting their system working again. Even worse is you spend time trying to figure out what you broke only to realise it was the config change that broke your system.

That's where Developer Settings comes into play. The idea is there is a global "default" set of data and then developers can have their own section in the config file that contains their custom settings. This way no other developer should touch their settings and merge conflicts should become an non issue.

Below is a quick example configuration file and how to load those settings:

  <ConfigurationGroup name="JonSettings">
    <ValueItems>
      <ValueItem key="TempPath" value="E:\workingDir\temp" />
    </ValueItems>
  </ConfigurationGroup>

  <ConfigurationGroup name="sqlExpress">
    <ValueItems>
      <ValueItem key="ConnectionStringName" value="sqlExpress" />
    </ValueItems>
  </ConfigurationGroup>

</Configs>

public class DevSettings
{
    public string ConnectionStringName { get; set; }
    public string TempPath { get; set; }
}

public class SettingsService 
{
    public DevSettings Get()
    {
        //By default it will use chaining and try its best to find an override
        var config = new ConfigurationLoader<DevSettings>();

        //if no key given you are using machine name only as appsettings and environmental Variable need to know what key to use
        return config.Create(); //a string can be passed into the constructor as a key for either appsettings or environmental variable.

        /**
        in the example above if an environmental variable was set for "Devkeys" Jon could set the value to be "JonSettings" to get the temp path override.
        The other two developers could set theirs to "sqlExpress".  
        Dave would not need to as no override is required.
        **/
    }

}

For more information please visit Developer Settings on Github. It is also available via Nuget.org

Published: Monday, 20 July 2015