Kralizek.Extensions.Configuration.Objects 3.0.0

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

// Install Kralizek.Extensions.Configuration.Objects as a Cake Tool
#tool nuget:?package=Kralizek.Extensions.Configuration.Objects&version=3.0.0

ObjectConfigurationExtensions

This repository contains a provider for Microsoft.Extensions.Configuration that allows the insertion of a concrete object into the configuration pipeline.

The library supports all primitive types, complex objects and sequences of both.

How to use it

Let's see it in action. Here is a simple ASP.NET Core application that loads an object in the configuration pipeline, specifically in the Test section.

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddObject(new
{
    Value = 123,
    ManyValues = new ComplexObject[]
    {
        new ("New value", 234),
        new ("Another value", 345)
    },
    Flag = true,
    Text = "Something"
}, "Test");

var app = builder.Build();

app.MapGet("/", (IConfiguration configuration) => configuration
    .GetSection("Test")
    .AsEnumerable()
    .OrderBy(c => c.Key)
    .ToDictionary(c => c.Key, v => v.Value));

app.Run();

public record ComplexObject(string Text, int Number);

You can install the package using the .NET CLI

$ dotnet add package Kralizek.Extensions.Configuration.Objects

When accessed at the root page, the application prints all the configuration values found in the Test section.

$ curl http://localhost:5003
{
  "Test":null,
  "Test:Flag":"true",
  "Test:ManyValues":null,
  "Test:ManyValues:0":null,
  "Test:ManyValues:0:Number":"234",
  "Test:ManyValues:0:Text":"New value",
  "Test:ManyValues:1":null,
  "Test:ManyValues:1:Number":"345",
  "Test:ManyValues:1:Text":"Another value",
  "Test:Text":"Something",
  "Test:Value":"123"
}

Root section name

The root section name used in the sample above is optional. If you prefer so, you can add the properties of the object directly to the root of the configuration.

builder.Configuration.AddObject(new
{
    IsEnabled = false
});

Newtonsoft.Json serializer

The library uses by default the JSON serializer available in the System.Text.Json namespace.

If you need to use the JSON serializer from Newtonsoft, you can install the specific package and use the AddObjectWithNewtonsoftJson method.

dotnet add package Kralizek.Extensions.Configuration.Objects.NewtonsoftJson
var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddObjectWithNewtonsoftJson(new
{
    Text = "Something"
}, "Test");

var app = builder.Build();

app.MapGet("/", (IConfiguration configuration) => configuration
    .GetSection("Test")
    .AsEnumerable()
    .OrderBy(c => c.Key)
    .ToDictionary(c => c.Key, v => v.Value));


app.Run();

Custom serializer

The serialization strategy doesn't play an important role in the library.

Yet, in case of need, a custom serializer implementing the interface Kralizek.Extensions.Configuration.IConfigurationSerializer can be provided.

The interface is very minimal and requires to write the object as a Dictionary<string, string?> where the key is the path to the property and the value is the value of the property.

public interface IConfigurationSerializer
{
    IDictionary<string, string?> Serialize(object source, string rootSectionName);
}

Let's assume we have a custom implementation

public class FailingConfigurationSerializer : IConfigurationSerializer
{
    public IDictionary<string, string?> Serialize(object source, string rootSectionName) => throw new NotImplementedException();
}

We can use it by passing an instance of the custom serializer to the AddObject method.

var builder = WebApplication.CreateBuilder(args);

var serializer = new FailingConfigurationSerializer();

builder.Configuration.AddObject(new
{
    Text = "Something"
}, serializer, "Test");

var app = builder.Build();

app.MapGet("/", (IConfiguration configuration) => configuration
    .GetSection("Test")
    .AsEnumerable()
    .OrderBy(c => c.Key)
    .ToDictionary(c => c.Key, v => v.Value));


app.Run();

Versioning

This library follows Semantic Versioning 2.0.0 for the public releases (published to the nuget.org).

How to build

This project uses Cake as a build engine.

If you would like to build this project locally, just execute the build.cake script.

You can do it by using the .NET tool created by CAKE authors and use it to execute the build script.

dotnet tool install -g Cake.Tool
dotnet cake
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Kralizek.Extensions.Configuration.Objects:

Package Downloads
Kralizek.Extensions.Configuration.Objects.NewtonsoftJson

Extension of Kralizek.Extensions.Configuration using Newtonsoft.Json.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 202 1/30/2024
2.0.1 6,146 6/16/2021
2.0.0 1,397 6/10/2021
1.0.1 3,346 11/29/2019
1.0.0 569 11/3/2019