DevTrends.ConfigurationExtensions 1.2.1

dotnet add package DevTrends.ConfigurationExtensions --version 1.2.1                
NuGet\Install-Package DevTrends.ConfigurationExtensions -Version 1.2.1                
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="DevTrends.ConfigurationExtensions" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DevTrends.ConfigurationExtensions --version 1.2.1                
#r "nuget: DevTrends.ConfigurationExtensions, 1.2.1"                
#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 DevTrends.ConfigurationExtensions as a Cake Addin
#addin nuget:?package=DevTrends.ConfigurationExtensions&version=1.2.1

// Install DevTrends.ConfigurationExtensions as a Cake Tool
#tool nuget:?package=DevTrends.ConfigurationExtensions&version=1.2.1                

DevTrends.ConfigurationExtensions

Test Status

DevTrends.ConfigurationExtensions on NuGet

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 26,586 4/6/2023
1.2.0 7,140 8/8/2022
1.1.0 1,508 5/26/2022
1.0.0 727 12/6/2021