How to create a hierarchical in-memory configuration in .Net Core
Asked Answered
S

3

9

I would like to create a hierarchical in-memory configuration along the lines of using a nested dictionary to populate the IConfiguration. My current approach is something like this.

I have a dictionary like this:

var inMemConfig = new Dictionary<string, object>();
inMemConfig["section1"] = new Dictionary<string, string>();
inMemConfig["section2"] = new Dictionary<string, object>();
inMemConfig["deepNest"] = new Dictionary<string, object>();
// Excluding a cast below: 
inMemConfig["deepNest"]["list"] = new List<Dictionary<string, string>>();
inMemConfig["deepNest"]["dict"] = new Dictionary<string string>();

After populating the above dictionary, I try to use ConfigurationBuilder like below.

var builder = new ConfigurationBuilder();
var configuration = builder.AddInMemoryCollection(inMemConfig).Build(); 

This obviously give the compiler error that inMemConfig needs to be of type: IEnumerable<KeyValuePair<string, string>>.

Is it not possible to create hierarchical in-memory configurations and if so, any pointers in the right direction are much appreciated. Thanks.

Shibboleth answered 12/3, 2019 at 7:3 Comment(0)
W
12

You can use a colon (":") as a delimiter to indicate hierarchy in your configuration. So for example:

var builder = new ConfigurationBuilder();
var configuration = builder.AddInMemoryCollection(new Dictionary<string, string> {
    ["deepNest:list"] = "some_nested_list_value",
    ["deepNest:dict"] = "some_nested_dict_value"
}).Build();
Wordsmith answered 3/4, 2020 at 3:30 Comment(0)
G
4

Here is an extension method I wrote that simplifies passing in a complex hierachical object representing settings:

/// <summary>
/// Adds settings to the builder from an anonymous object. This allows complex settings object to be built and passed into here, e.g.
/// <code>
///var settings = new
///{
///  programmeLogin = new[] {
///    new {
///      programmeId = 1,
///      loginOptions = new {
///        loginType = loginType,
///        isDefault = true
///      }
///    }
///  },
///  portal = new {
///    programmeId = 1
///  }
///};
///
///IConfigurationBuilder builder = new ConfigurationBuilder().AddObject(settings);
///IConfigurationRoot configurationRoot = builder.Build();
/// </code>
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="obj">The <see cref="object"/> containing the settings to add.</param>
/// <returns></returns>
public static IConfigurationBuilder AddObject(this IConfigurationBuilder builder, object obj)
{
    string json = System.Text.Json.JsonSerializer.Serialize(obj);

    return builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json)));
}
Guipure answered 17/5, 2023 at 9:45 Comment(0)
M
1

var inMemConfig = new Dictionary<string, string>();

...

[deepNest:0:list]="firstL",

[deepNest:1:list]="secondL",

[deepNest:0:dict]="firstD",

[deepNest:1:dict]="secondD"

...

Mania answered 19/10, 2021 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.