Skip to content

Provider implementation for Microsoft.Extensions.Configuration to add concrete objects to the configuration pipeline

License

Notifications You must be signed in to change notification settings

Kralizek/ObjectConfigurationExtensions

Repository files navigation

Build status NuGet version

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

About

Provider implementation for Microsoft.Extensions.Configuration to add concrete objects to the configuration pipeline

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Languages