Allow IValidatableObject implementors and ValidationAttribute derivatives to access the IServiceProvider #3746
Description
Because of the removal of the ability to use the FromServices attribute on model properties, and considering the current lack of support for constructors or dependency injection of any sort on models, there is no longer a way for models implementing the IValidatableObject interface to make use of services for validation purposes.
I would like to propose a very simple idea to fill in that gap: have the model binding system pass the IServiceProvider into the constructor of the ValidationContext during validation.
- See where it would be done for IValidatableObject here.
- See where it would be done for ValidationAttribute here.
Since ASP.NET 5 and the ValidationContext use the exact same IServiceProvider interface, developers would even be able to make use of the extension methods then (GetRequiredService, etc.)
If this is agreeable I would be happy to do the required work. Of course if anyone proposes an alternative solution to allow models or attributes to use services for validation purposes, I would be glad to hear it.