Turn this:
var mySetting = ConfigurationManager.AppSettings["MySetting"];
into this:
var mySetting = Configuration.AppSettings.MySetting;
- Supports mapping from appSettings, connectionStrings and environment variables.
- No magic strings - avoid run-time errors from typos.
- Provides an interfaced POCO (plain old CLR object), allowing your strongly-typed
IConfiguration
to be injected anywhere in a unit-testable manner. - Refactor-friendly - renaming settings is simple.
- Find usages easily.
NuGet
Install fromPM> 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:
<appSettings>
<add key="KeyInAppSettings" value="Foo"/>
<add key="WorksWithManyTypes" value="42.0"/>
<add key="AlsoWorksWithEnums" value="Bar"/>
</appSettings>
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.
ConfigMapper.Map<IMyEnvironmentVariables>(MapFrom.EnvironmentVariables);
Map from custom keys
ConfigMapper will look for a key name matching each property name. To override this, use the MapFrom
attribute
[MapFrom("ActualKey")]
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.
[Optional]
string ThisWillBeNull { get; }
[Optional("true")]
bool ThisDefaultsToTrue { get; }
Efficiency
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.
Licence
Provided under the MIT Licence
Contact
You're welcome to contact me at any time with questions, comments and ideas.