You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The IValidatorFactory interface and its implementors provided a generic mechanism for instantiating a validator. This was implemented over 12 years ago back when Dependency Injection was less common in .NET and prior the IServiceProvider being shipped as part of .NET
We now recommend using the .NET IServiceProvider directly (or another DI container of your choice)
Migration
Register your validators with the .NET service collection (or a DI container of your choice)
Resolve the validators directly from the service provider (or DI container)
Note that the IValidatorFactory expected you to pass in the model type, but when working with a service provider you should pass in the validator type.
// Before: Using a validator factory (generic). IValidatorFactoryfactory= ...IValidator<Person>validator=factory.GetValidator<Person>();// After - using a service provider (generic)IServiceProviderserviceProvider= ...IValidator<Person>validator=serviceProvider.GetService<IValidator<Person>>();// Before: Using a validator factory (non-generic)IValidatorFactoryfactory= ...IValidator validator =factory.GetValidator(typeof(Person));// After: Using a service provider (non-generic)IServiceProviderserviceProvider= ...
Type genericType=typeof(IValidator<>).MakeGenericType(typeof(Person));IValidatorvalidator=serviceProvider.GetService(genericType);
The text was updated successfully, but these errors were encountered:
Background
The
IValidatorFactory
interface and its implementors provided a generic mechanism for instantiating a validator. This was implemented over 12 years ago back when Dependency Injection was less common in .NET and prior theIServiceProvider
being shipped as part of .NETWe now recommend using the .NET
IServiceProvider
directly (or another DI container of your choice)Migration
Note that the
IValidatorFactory
expected you to pass in the model type, but when working with a service provider you should pass in the validator type.The text was updated successfully, but these errors were encountered: