View on GitHub


Simple .NET config to object mapping with no magic strings

Download this project as a .zip file Download this project as a tar.gz file

Turn this:

var mySetting = ConfigurationManager.AppSettings["MySetting"];

into this:

var mySetting = Configuration.AppSettings.MySetting;

Install from NuGet

PM> Install-Package ConfigMapper

Usage is simple.

using ConfigMapping;
public interface IAppSettings
    string KeyInAppSettings { get; }
    decimal WorksWithManyTypes { get; }
    MyEnum AlsoWorksWithEnums { get; }
IAppSettings configuration = ConfigMapper.Map<IAppSettings>();

Configure your DI to inject IAppSettings using the Map method into any class. The code above assumes we defined MyEnum somewhere and we have our config file as:

  <add key="KeyInAppSettings" value="Foo"/>
  <add key="WorksWithManyTypes" value="42.0"/>
  <add key="AlsoWorksWithEnums" value="Bar"/>

Any Type the System.Convert class can handle is accepted, in addition to enums.

Prefer to use without injection? That's fine too.

Place this class in your project root for ease of access and call it from anywhere:

public static class Configuration
  public static IAppSettings AppSettings = ConfigMapper.Map<IAppSettings>();
var myConfigValue = Configuration.AppSettings.MyConfigKey;

Connection strings are also handled

public interface IConnectionStrings
    string JustTheConnectionStringItself { get; }
    ConnectionStringSettings AllConnectionStringProperties { get; }
var cs = ConfigMapper.Map<IConnectionStrings>(MapFrom.ConnectionStrings);

Simply match your property names to your connection string names. Use string property types for simple mappings or ConnectionStringSettings property types to access the ProviderName and other properties.

Map from environment variables

Environment variables are mapped at the process level - any user or system environment variables will also be accessible. Include only the variables you need to map in your interface, the rest are ignored.


Map from custom keys

ConfigMapper will look for a key name matching each property name. To override this, use the MapFrom attribute

    string AnythingYouLike { get; }

Mark optional properties

To be explicit ConfigMapper throws exceptions if it comes across missing keys in your config. You can mark properties as optional if this is actually intentional. To use anything other than the default value for each type you can specify this too, it will be converted from a string to your type like other configuration values.

    string ThisWillBeNull { get; }

    bool ThisDefaultsToTrue { get; }


ConfigMapper is thread-safe, and only one concrete instance from an interface is ever generated, meaning you can call the Map<T> method any number of times in your code without increased memory usage.

Config Change Handling

Call ConfigMapper.RefreshConfiguration if your configuration has changed. Any previously mapped objects will be updated with the latest values from your configuration without needing to re-map them. This works with appSettings and connectionStrings but not with environment variables.


Provided under the MIT Licence


You're welcome to contact me at any time with questions, comments and ideas.