Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

About

The SmartDev.ConfigurationMapper is a small library that helps you map your ASP.NET 5 configuration to classes. It is useful when you want an easy way to access your config values strongly typed and with well defined default values.

Current Version is built on ASPNET5 Beta3.

Just use:

PM> Install-Package SmartDev.ConfigurationMapper -Pre

NuGet: SmartDev.ConfigurationMapper

Build status

CI is hosted on AppVeyor, Project /gingters/smartdev-configurationmapper.
Builds: Build status Build status

Usage

Most basic

Assume this config.json:

{
    "SomeValue": "Hello World",
    "SomeIntValue": 42
}

You define your configuration objects as POCOs. Make sure, your property names match the keys in the config. Like this:

public class MyConfig
{
    public string SomeValue { get; set; }
    public int SomeIntValue { get; set; }
}

Based on the IConfiguration data, you can map the values to your class:

// var config = an instance of IConfiguration, i.e. from DI
var myConfig = config.Map<MyConfig>();

Access your config:

// Will print 42 lines of 'Hello World'
for (var i = 0; i < myConfig.SomeIntValue; i++)
{
    Console.WriteLine(myConfig.SomeTextValue);
}

Alternative Keys

You can map your properties to other Key names by adding the KeyAttribute to it. So, given the same config, your can give the properties on your class more meaningful names:

public class MyConfig
{
    [Key("SomeValue")]
    public string TextToPrint { get; set; }
    [Key("SomeIntValue")]
    public int LinesToPrint { get; set; }
}

Scoping

In Microsoft.Framework.ConfigurationModel it is possible to scope your configuration down in a hiearchy. Either by nesting Json objects or by separating your key names with colons. You can scope your class to a certain sub-key by either adding the ScopeAttribute to your class:

[Scope("Some:Sub:Key")]
public class MyConfig
{
    public string TextToPrint { get; set; }
    public int LinesToPrint { get; set; }
}

or directly by specifying the scope in the call to Map:

var myConfig = config.Map<MyConfig>("Sub:Key");

Roadmap / Further plans

First of all, documentation is a top prio task for this library. Then I want to introduce an optional base class for your configuration values, that knows about the ConfigurationModel and the mapper, and can be updated when config values change. This way, you could add updateable strongly typed configuration objects directly into your DI.

LuisRudge thought about adding support for arrays.

Other stuff

Interesting Background info on ConfigurationModel

Dependencies

Rules for this project

  • This project sticks to the corefx Coding style with two important exceptions:
    • Indentation is done with tabs. No arguments about that.
    • When calling methods on types, DON'T use the C# alias (i.e. use String.IsNullOrEmpty() instead of string.IsNullOrEmpty()).

About

A small ASP.NET 5 configuration to class mapper. Inspired by LuisRudge's Configurator.

Resources

License

Releases

No releases published

Packages

No packages published