Static class mapping

All fields must be public static properties with getter and setter methods.
public class MyConfig
    public static string StringValue { get; set; }

Initializing values

On application start call
This will set public static Properties in MyConfig class. If some evaluation fails, exception will be thrown with validation errors.

Interface mapping

  • Allows to create read-only properties
  • Re-use configuration sections.
  • Scopes and ovverrides

Configuration mapping

public interface ICfg
    string StringValue { get; }
    ICfgSection Foo { get; }
    ICfgSection Bar { get; }

public interface ICfgSection 
    string Key { get; }
    string Value { get; }


<add key="StringValue " value="qwerty" />
<add key="Foo.Key" value="x" />
<add key="Foo.Value" value="y" />
<add key="Bar.Key" value="X" />
<add key="Bar.Value" value="Y" />

Using scope

// In app start

// Fetch created proxy from local storage
// If type was not initialized, it will be initialized. 
// But it is strongly recommended that initialization is done in app start
var cfg = AppSettings.Get<ICfg>();

using (var scope = new CfgScope<ICfg>())
    scope.Override(x => x.Foo.Value, "Z");
    // cfg.Foo.Value is now "Z"
// cfg.Foo.Value is "y"


There are helper attributes that give you the control over configuration values

Creating custom validator

Must be inherited from either CfgFinalizedStringValueValidatorBaseAttribute or CfgValueValidatorBaseAttribute

As names say CfgFinalizedStringValueValidatorBaseAttribute validates finalized string value and CfgValueValidatorBaseAttribute validates parsed value.

Sample finalized string validator
public class MustBetXYorZAttribute : CfgFinalizedStringValueValidatorBaseAttribute
    protected override void Validate(string finalizedStringValue)
        if (!new [] { "X", "Y", "Z" }.Contains(finalizedStringValue))
            throw new Exception("Value must be X, Y or Z");


This feature is profitable if same configuration is used in multiple projects and different projects have different set of constraints. For instance same core library is used by web application and windows service.
Each validation attribute accepts contexts. This means in which context config value is validated. If contexts are not set, validations will apply in all contexts.
Current context is set with initialization.

Other attributes

Helper config values

There are few helper configuration value objects:


Config values can use other values. You can use variables beytween % symbols

<add key="root" value="C:/temp/root/" />
<add key="images" value="%root%images/" />

Custom parsers

Need object transformation not bundled in cfg? You can user your own parser.
For example CultureInfo:
AppSettings.Settings.AddParser(x => new CultureInfo(x));
This registers new parser for returned value type. If you configuration contains property with type CultureInfo, this parser fill be used.

Last edited Nov 8, 2013 at 10:43 AM by maxlego, version 12


No comments yet.