Populate POCO class with config values - eaardal/konfiggy GitHub Wiki

With version 1.0.6 you can automatically map a POCO class to appSetting`connectionString` keys.

With a POCO class like

class Config
{
    public string MySetting { get; set; }
    public string MyConnectionString { get; set; }
}

And a app.config file like

<appSettings>
  <add key="dev.mySetting" value="foo"/>
</appSettings>
<connectionStrings>
  <add name="dev.myConnectionString" connectionString="bar"/>
</connectionStrings>

You can automatically populate it with config data

var config = konfiggy.PopulateConfig<Config>()
                     .WithAppSettings()
                     .WithConnectionStrings()
                     .Populate();

The mapping is done by reflecting on the POCO's properties name and appSetting`connectionString` keys in the config file. The mapping is not case sensitive.

Or map the properties manually if the names doesn't match exactly

var config = konfiggy.PopulateConfig<Config>()
                     .WithAppSettings(c => c.Map(x => x.MySetting, "someAppSettingKey"))
                     .WithConnectionStrings(c => c.Map(x => x.MyConnectionString, "someConnectionStringName"))
                     .Populate();

Supported types

All primitive types are supported, such as int, bool, double, char, etc.

IEnumerable support

The config

<add key="items" value="foo;bar;hello;world"/>

Maps to the property public IEnumerable<string> Items { get; set; }

This also works with other primitive types:

<add key="intItems" value="1; 2; 3; 4;"/>
<add key="doubleItems" value="1.12; 2.34; 3.56; 4.78"/>
<add key="boolItems" value="true; false; true; false"/>

Maps to

public IEnumerable<int> IntItems { get; set; }
public IEnumerable<double> DoubleItems { get; set; }
public IEnumerable<bool> BoolItems { get; set; }
⚠️ **GitHub.com Fallback** ⚠️