Add support for stripping chars while binding to properties using ConfigurationBinder
#37378
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-Extensions-Configuration
Milestone
Uh oh!
There was an error while loading. Please reload this page.
Background and Motivation
When using
IOptions<T>
, the property names must match case-insensitive with the keys stored in the instance ofIConfiguration
/IConfigurationProvider
based on the rules defined inConfigurationBinder
. This can cause some readability concerns with environment variables that have single underscores (as not to conflict with the delimiter with 2 underscores) and all caps names as is a common convention. There is no way to override the behavior by configuration or reflection other that to change how the keys are stored in theIConfigurationProvider
s.For instance, it maybe advantageous to allow applications to take keys such as
My_Configuration_Key
and bind it to a property namedMyConfigurationKey
in a model forIOptions<T>
or other uses that theConfigurationBinder
provides.I believe the logic should be contained in
ConfigurationBinder
and done procedurally (perhaps with some sort of caching mechanism) vs. reflection with a custom attribute declared on the properties of the options model classes or adjustingIConfigurationProvider
s to implement similar logic.Proposed API
I propose a new property for
BinderOptions
:KeyCharsToStrip
, defaulting tonull
indicating no stripping of characters from the key:namespace Microsoft.Extensions.Configuration { public class BinderOptions { + public char[]? KeyCharsToStrip { get; set; } = null; }
When
BinderOptions
was designed, it was designed to be extensible and further refine the behavior of theConfigurationBinder
.Usage Examples
When calling
IConfiguration.Bind
there exist overloads that allows one to pass a lambda of typeAction<BinderOptions>
to configure theConfigurationBinder
. Hence, theKeyCharsToStrip
property would be exposed there from an application perspective.Internally to
ConfigurationBinder
, there would be some sort of normalization step while attempting to locate the key to match the property name.Alternative Designs
One alternative solution to normalizing the configuration keys would be to allow applications to declare an attribute on the property they wish to map much like
JsonProperty
:The application could consume it this way:
However, this is complicated by a few reasons that I can think of off the top of my head:
IServiceCollection
work..Bind
?Another alternative solution would be to add the option to each of the
IConfigurationProvider
s' registration. Here are a few concerns with this approach:IConfiguration
does. It is more of a concern of the "view" thatConfigurationBinder
provides.Risks
BinderOptions
is an additive change, no breaking changes claimed.ConfigurationBinder
, there will be a hit to memory/performance depending on the size of the inputs (keys and the chars inKeyCharsToStrip
. If the option is not set, the logic will simply skip the costly operation of calculating the key to property name binding.The text was updated successfully, but these errors were encountered: