Description
Background and Motivation
To support Keyed Services in MVC, we need to make a small change to BindingInfo
.
Proposed API
namespace Microsoft.AspNetCore.Http;
public class BindingInfo
{
+ public object? ServiceKey { get; set; }
}
Usage Examples
This property will be set by calling BindingInfo.GetBindingInfo(IEnumerable<object> attributes)
, like other existing property (PropertyFilterProvider
, RequestPredicate
, etc.)
Attributes are already extracted for this method.
Alternative Designs
Without this property, we will need to do some reflection for all binding that comes from the service provider.
var attribute = context.Metadata.Identity.ParameterInfo?.GetCustomAttribute<FromKeyedServicesAttribute>();
if (attribute?.Key == null)
{
// existing path
}
else
{
// keyed service path
}
This will be expensive, since it will be done for all parameter that binds from the service provider.
Risks
A custom implementation of IModelBinder
that target BindingSource.Services
could resolve a non-keyed service when a keyed one was requested if they don't check BindingInfo.ServiceKey
. But that's also the reason why I think this property needs to be public and not internal.
I don't expect a performance hit since the attributes are already populated when calling BindingInfo.GetBindingInfo
.