Stronk 4.0.0
See the version list below for details.
dotnet add package Stronk --version 4.0.0
NuGet\Install-Package Stronk -Version 4.0.0
<PackageReference Include="Stronk" Version="4.0.0" />
paket add Stronk --version 4.0.0
#r "nuget: Stronk, 4.0.0"
// 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 PropertyDescriptor
s for it.
Stronk comes with two implementations of IPropertySelector
, which are both enabled by default:
- PrivateSetterPropertySelector - this will select any public property which can be written to.
- BackingFieldPropertySelector) - this will select any public property with a backing field which matches the property name (optionally with a preceding underscore.)
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 onPropertyDescriptor.Name
, if there is no match inAppSettings
, it will try theconnectionStrings
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
andEnum.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 bothApp.Config
andWeb.Config
are supported out of the box.
To Do
- Customisable error policy
- no value found
- no converter found
- converter was not happy
Product | Versions 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. |
-
.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.