Open
Description
Background and motivation
We should add a new HasChildren
property to IConfiguration
. An implementation given existing APIs might look like this:
public static bool HasChildren(IConfiguration configuration)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
return true;
}
return false;
}
This implementation makes allocations which is not ideal. We could get better perf with a new API with a more optimal framework implementation.
API Proposal
namespace Microsoft.Extensions.Configuration
{
public interface IConfiguration
{
+ public bool HasChildren { get; }
}
}
API Usage
A primary user of this feature would be the configuration binding source generator. Sample code it would generate:
private static void BindCore(IConfiguration configuration, MyClass obj)
{
ThrowIfNull(obj);
if (configuration["MyString"] is string stringValue0)
{
obj.MyString = stringValue0;
}
IConfigurationSection section2 = configuration.GetSection("MyList");
if (section2.HasChildren)
{
List<int> temp3 = obj.MyList;
temp3 ??= new List<int>();
BindCore(section2, ref temp3);
obj.MyList = temp3;
}
IConfigurationSection section4 = configuration.GetSection("MyDictionary");
if (section4.HasChildren)
{
Dictionary<string, string> temp5 = obj.MyDictionary;
temp5 ??= new Dictionary<string, string>();
BindCore(section4, ref temp5);
obj.MyDictionary = temp5;
}
// Repeat for every object or collection property on MyClass
}
We see that the binding performance would improve with a more optimal implementation, particularly for large object graphs.
cc @tarekgh @eerhardt @davidfowl @dotnet/area-extensions-configuration