Wiki

Clone wiki

Simple configuration wrapper / Home

Welcome to simple configuration wrapper

Overview

The idea of the simple configuration wrapper is to wrap configuration of your .NET application in with strong types, information about format and purpose. Furthermore som validation and autodocumentation features.

It basically is a wrapper around all those ad-hoc (error prone) app settings we all have a tendency to introduce.

Example

Declare ConfigurationWrapper

#!csharp
public class ExampleConfiguration : ConfigurationWrapper
{
  public ConfigurationEntry<int> SomeInt { get; } = new PrimitiveEntry<int>(identifier: "SomeIntWithAName", defaultValue: 4, description: "An int used for retries or similar");    
}
// Can also create and use entries directly, although this defeats the purpose a bit:
//... (some class)

public class SomeEstablisher {
  private void EstablishConnection() {
     int timeOutInSeconds = new PrimitiveEntry<int>(identifier: "GlobalTimeOutInSeconds", defaultValue: 30); 
     //Etc.....
  }
}

And use it like this:

#!csharp
public class ExampleUsage
{
    private readonly ExampleConfiguration _config; 
    public ExampleUsage(ExampleConfiguration config) {
        _config = config;
    }
    //does not need to be injected
    public ExampleUsage() : this(new ExampleConfiguration()){

    }

    public bool DoSomething() {
        //Implicit conversion ensures correct type 
        if (_config.SomeInt < 3) return true; 
        else return false;
    }
}
#!xml
<configuration>
  <appSettings>
    <!-- Config entry info: : An int used for retries or similar  -->
    <add key="SomeIntWithAName" value="2"/>
  </appSettings>
</cofiguration>

Configuration entries

  • StringEntry : ConfigurationEntry<String>
    • A simple string value.
  • PipeSeperatedStringList : ConfigurationEntry<List\<string>>
    • A list of strings. Seperator char can be passed along to it.
  • PrimitiveEntry<T> : ConfigurationEntry<T> where T : IConvertible
    • A simple convertible value. Primitives, enums, datetime.
  • PrimitivesListEntry<T> : ConfigurationEntry<List\<T>> where T : IConvertible
    • A list of simple convertibles.
  • TimeSpanEntry : ConfigurationEntry<TimeSpan>
    • A timespan value.
  • DictionaryEntry<TKey, TValue> : ConfigurationEntry<Dictionary\<TKey, TValue>> where TKey : IConvertible where TValue : IConvertible
    • A dictionary from and to convertibles. Seperator-char for items and key value can be provided.
  • DepInjEntry<TSource> : ConfigurationEntry<Type>
    • Configure a Dependency Injection mapping. From an interface to a subtype.

Updated