DevTrends.ConfigurationExtensions
1.2.0
See the version list below for details.
dotnet add package DevTrends.ConfigurationExtensions --version 1.2.0
NuGet\Install-Package DevTrends.ConfigurationExtensions -Version 1.2.0
<PackageReference Include="DevTrends.ConfigurationExtensions" Version="1.2.0" />
paket add DevTrends.ConfigurationExtensions --version 1.2.0
#r "nuget: DevTrends.ConfigurationExtensions, 1.2.0"
// Install DevTrends.ConfigurationExtensions as a Cake Addin #addin nuget:?package=DevTrends.ConfigurationExtensions&version=1.2.0 // Install DevTrends.ConfigurationExtensions as a Cake Tool #tool nuget:?package=DevTrends.ConfigurationExtensions&version=1.2.0
DevTrends.ConfigurationExtensions
This package provides a couple of extension methods that allow you to bind configuration to records and classes with parameterized (non parameterless) constructors.
It also works nicely with nullable types and nullable reference types (NRTs) and should help you avoid some of those annoying 'possible null reference' warnings.
The package works on the 'fail fast' principle and any missing non-nullable configuration values will result in an exception at startup.
Note that the package extends the built-in configuration so it will pick up settings from all the usual sources such as appsettings, environment variables etc.
Installation
dotnet add package DevTrends.ConfigurationExtensions
Usage
Given a record:
public record Foo(string Bar, int Blah)
If you have the following in your appsettings:
"Foo": {
"Bar": "hello",
"Blah": 42
}
You can bind the record to the settings by using:
builder.Configuration.Bind<Foo>()
You will also need to add the following namespace to program.cs:
using DevTrends.ConfigurationExtensions;
Dependency Injection
You will probably want to register the strongly typed configuration classes with the DI container so instead of using the above, you can use:
builder.Services.AddSingleton(builder.Configuration.Bind<Foo>());
What is supported?
You can bind to properties of the following type (plus nullable version including nullable reference types such as string?):
- string
- int
- bool
- decimal
- datetime
- uri
- nested classes
Your classes or records must have a public constructor which sets all properties that you wish to automatically populate.
If you have any optional configuration values, be sure to use nullable types.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Configuration (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.