|
1 | | -# ObjectConfigurationExtensions |
| 1 | +<a href="https://www.buymeacoffee.com/rengol"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a pizza&emoji=🍕&slug=rengol&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" /></a> |
| 2 | + |
| 3 | +[](https://ci.appveyor.com/project/Kralizek/objectconfigurationextensions/branch/master) [](https://www.nuget.org/packages/Kralizek.Extensions.Configuration.Objects) |
| 4 | + |
| 5 | +# ObjectConfigurationExtensions |
| 6 | + |
| 7 | +This repository contains a provider for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration/) that allows the insertion of a concrete object into the configuration pipeline. |
| 8 | + |
| 9 | +The library supports all primitive types, complex objects and sequences of both. |
| 10 | + |
| 11 | +## How to use it |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +```csharp |
| 16 | +var builder = WebApplication.CreateBuilder(args); |
| 17 | + |
| 18 | +builder.Configuration.AddObject(new |
| 19 | +{ |
| 20 | + Value = 123, |
| 21 | + ManyValues = new ComplexObject[] |
| 22 | + { |
| 23 | + new ("New value", 234), |
| 24 | + new ("Another value", 345) |
| 25 | + }, |
| 26 | + Flag = true, |
| 27 | + Text = "Something" |
| 28 | +}, "Test"); |
| 29 | + |
| 30 | +var app = builder.Build(); |
| 31 | + |
| 32 | +app.MapGet("/", (IConfiguration configuration) => configuration |
| 33 | + .GetSection("Test") |
| 34 | + .AsEnumerable() |
| 35 | + .OrderBy(c => c.Key) |
| 36 | + .ToDictionary(c => c.Key, v => v.Value)); |
| 37 | + |
| 38 | +app.Run(); |
| 39 | + |
| 40 | +public record ComplexObject(string Text, int Number); |
| 41 | +``` |
| 42 | + |
| 43 | +You can install the package using the .NET CLI |
| 44 | + |
| 45 | +```bash |
| 46 | +$ dotnet add package Kralizek.Extensions.Configuration.Objects |
| 47 | +``` |
| 48 | + |
| 49 | +When accessed at the root page, the application prints all the configuration values found in the `Test` section. |
| 50 | + |
| 51 | +```bash |
| 52 | +$ curl http://localhost:5003 |
| 53 | +{ |
| 54 | + "Test":null, |
| 55 | + "Test:Flag":"true", |
| 56 | + "Test:ManyValues":null, |
| 57 | + "Test:ManyValues:0":null, |
| 58 | + "Test:ManyValues:0:Number":"234", |
| 59 | + "Test:ManyValues:0:Text":"New value", |
| 60 | + "Test:ManyValues:1":null, |
| 61 | + "Test:ManyValues:1:Number":"345", |
| 62 | + "Test:ManyValues:1:Text":"Another value", |
| 63 | + "Test:Text":"Something", |
| 64 | + "Test:Value":"123" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Root section name |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +```csharp |
| 73 | +builder.Configuration.AddObject(new |
| 74 | +{ |
| 75 | + IsEnabled = false |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Newtonsoft.Json serializer |
| 80 | + |
| 81 | +The library uses by default the JSON serializer available in the `System.Text.Json` namespace. |
| 82 | + |
| 83 | +If you need to use the JSON serializer from Newtonsoft, you can install the specific package and use the `AddObjectWithNewtonsoftJson` method. |
| 84 | + |
| 85 | +```bash |
| 86 | +dotnet add package Kralizek.Extensions.Configuration.Objects.NewtonsoftJson |
| 87 | +``` |
| 88 | + |
| 89 | +```csharp |
| 90 | +var builder = WebApplication.CreateBuilder(args); |
| 91 | + |
| 92 | +builder.Configuration.AddObjectWithNewtonsoftJson(new |
| 93 | +{ |
| 94 | + Text = "Something" |
| 95 | +}, "Test"); |
| 96 | + |
| 97 | +var app = builder.Build(); |
| 98 | + |
| 99 | +app.MapGet("/", (IConfiguration configuration) => configuration |
| 100 | + .GetSection("Test") |
| 101 | + .AsEnumerable() |
| 102 | + .OrderBy(c => c.Key) |
| 103 | + .ToDictionary(c => c.Key, v => v.Value)); |
| 104 | + |
| 105 | + |
| 106 | +app.Run(); |
| 107 | +``` |
| 108 | + |
| 109 | +## Custom serializer |
| 110 | + |
| 111 | +The serialization strategy doesn't play an important role in the library. |
| 112 | + |
| 113 | +Yet, in case of need, a custom serializer implementing the interface `Kralizek.Extensions.Configuration.IConfigurationSerializer` can be provided. |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +```csharp |
| 118 | +public interface IConfigurationSerializer |
| 119 | +{ |
| 120 | + IDictionary<string, string?> Serialize(object source, string rootSectionName); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +Let's assume we have a custom implementation |
| 125 | + |
| 126 | +```csharp |
| 127 | +public class FailingConfigurationSerializer : IConfigurationSerializer |
| 128 | +{ |
| 129 | + public IDictionary<string, string?> Serialize(object source, string rootSectionName) => throw new NotImplementedException(); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +We can use it by passing an instance of the custom serializer to the `AddObject` method. |
| 134 | + |
| 135 | +```csharp |
| 136 | +var builder = WebApplication.CreateBuilder(args); |
| 137 | + |
| 138 | +var serializer = new FailingConfigurationSerializer(); |
| 139 | + |
| 140 | +builder.Configuration.AddObject(new |
| 141 | +{ |
| 142 | + Text = "Something" |
| 143 | +}, serializer, "Test"); |
| 144 | + |
| 145 | +var app = builder.Build(); |
| 146 | + |
| 147 | +app.MapGet("/", (IConfiguration configuration) => configuration |
| 148 | + .GetSection("Test") |
| 149 | + .AsEnumerable() |
| 150 | + .OrderBy(c => c.Key) |
| 151 | + .ToDictionary(c => c.Key, v => v.Value)); |
| 152 | + |
| 153 | + |
| 154 | +app.Run(); |
| 155 | +``` |
| 156 | + |
| 157 | +## Versioning |
| 158 | + |
| 159 | +This library follows [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html) for the public releases (published to the [nuget.org](https://www.nuget.org/)). |
| 160 | + |
| 161 | +## How to build |
| 162 | + |
| 163 | +This project uses [Cake](https://cakebuild.net/) as a build engine. |
| 164 | + |
| 165 | +If you would like to build this project locally, just execute the `build.cake` script. |
| 166 | + |
| 167 | +You can do it by using the .NET tool created by CAKE authors and use it to execute the build script. |
| 168 | + |
| 169 | +```powershell |
| 170 | +dotnet tool install -g Cake.Tool |
| 171 | +dotnet cake |
| 172 | +``` |
0 commit comments