Stronk 4.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Stronk --version 4.0.0                
NuGet\Install-Package Stronk -Version 4.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Stronk" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stronk --version 4.0.0                
#r "nuget: Stronk, 4.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Stronk as a Cake Addin
#addin nuget:?package=Stronk&version=4.0.0

// Install Stronk as a Cake Tool
#tool nuget:?package=Stronk&version=4.0.0                

Stronk

Mapping app.config an web.config to strong typed objects

Usage:

public class MyApplicationConfiguration
{
    public string ApplicationName { get; private set; }
    public int ApiVersion { get; private set; }
    public ConfigurationMode Mode { get: private set; }

    public string MainDB { get; set; }

    public MyApplicationConfiguration()
    {
        this.FromAppConfig();

        // this is identical to FromAppConfig, it's here to make you happy if you are reading a web.config
        //this.FromWebConfig():

        // this is an idea for .net core. Subject to change. a lot.
        //this.FromConfigurationProviderThingy(itGoesHere.Build());
    }
}

public enum ConfigurationMode
{
    Local,
    Dev,
    Test,
    QA,
    ExternalTest,
    Production
}

App.Config or Web.Config

<configuration>
  <appSettings>
    <add key="ApplicationName" value="testing" />
    <add key="ApiVersion" value="12" />
    <add key="Mode" value="QA" />
  </appSettings>
  <connectionStrings>
    <add name="MainDb" connectionString="Some Connection String Here" />
  </connectionStrings>
</configuration>

Customisation

You can configure how Stronk handles configuration, and where Stronk reads it from.

The rough pipeline is as follows:

Select Properties to be populated → Get a Value from ConfigurationSource for each Property → Find an IValueConverter for the property → Convert the value with the selected converter → Assign to the property

To specify your own conversion and mapping you can either implement IStronkOptions, or use the customisation methods on StronkOptions, e.g.

var sc = new StronkOptions();
sc.AddBefore<EnumValueConverter>(new SpecialEnumValueConverter());

this.FromAppConfig(sc);

Logging

Logging is supported by means of a single callback, providing a template, and an array of objects. The template is designed for serilog (or other structured logging), in that it uses named substitutions rather than index based, for example Converting '{value}' and assigning to {typeName}.{propertyName}.

var sc = new StronkOptions
{
    Logger = message => Serilog.Log.Information(message.Template, message.Args)
};

this.FromAppConfig(sc);

Customising Conversion and Mapping

Property Selection

This is used to scan the target Type and provide a set of PropertyDescriptors for it.

Stronk comes with two implementations of IPropertySelector, which are both enabled by default:

In your own implementation, you just need to return an enumerable of PropertyDescriptor:

new PropertyDescriptor
{
	Name = prop.Name,
	Type = prop.PropertyType,
	Assign = (target, value) => prop.GetSetMethod(true).Invoke(target, new[] { value })
}
Value Selection

This is used to match a PropertyDescriptor to a value provided by IConfigurationSource.

Stronk comes with one implementation of ISourceValueSelector, which is enabled by default:

  • PropertyNameSourceValueSelector - this will return a value from the AppSettings section of the app.config file, matching on PropertyDescriptor.Name, if there is no match in AppSettings, it will try the connectionStrings section also.
Value Conversion

This is used to take the value from an ISourceValueSelector and convert it to the type from PropertyDescriptor.

Stronk comes with many converters, which are attempted to be used in order of specification. By default this is the conversion order:

  • Uri - calls val, type => new Uri(val)
  • Guid - calls val, type => Guid.Parse(val)
  • EnumValueConverter - Makes use of Enum.Parse and Enum.IsDefined
  • CsvValueConverter - calls other value converters to convert individual values
  • FallbackValueConverter - calls val, type => Convert.ChangeType(val, type)

The easiest way of creating a new converter is to just add an instance of LambdaValueConverter<T> to the IStronkOptions. This is how Uri and Guid are implemented.

Customising Configuration Source

This is used to customise where Stronk will read configuration values from.

Stronk comes with one implementation of IConfigurationSource:

  • AppConfigSource - this uses the ConfigurationManager class, which means both App.Config and Web.Config are supported out of the box.

To Do

  • Customisable error policy
    • no value found
    • no converter found
    • converter was not happy
Product Compatible and additional computed target framework versions.
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Stronk:

Package Downloads
Stronk.Source.Consul

Consul configuration source for Stronk.

Stronk.Validation.FluentValidation

FluentValidation extensions for Stronk.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.4.0 1,360 10/14/2018
5.3.1 834 8/26/2018
5.3.0 797 8/26/2018
5.2.0 867 8/10/2018
5.1.0 1,058 5/4/2018
5.0.0 916 4/17/2018
4.0.0 1,463 10/17/2017
3.0.0 1,158 7/2/2017
2.0.0 1,049 12/29/2016
1.0.2 1,058 12/9/2016
1.0.1 992 12/7/2016
1.0.0 967 12/5/2016