-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
Description
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 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).
IValidatorFactory factory = ...
IValidator<Person> validator = factory.GetValidator<Person>();
// After - using a service provider (generic)
IServiceProvider serviceProvider = ...
IValidator<Person> validator = serviceProvider.GetService<IValidator<Person>>();
// Before: Using a validator factory (non-generic)
IValidatorFactory factory = ...
IValidator validator = factory.GetValidator(typeof(Person));
// After: Using a service provider (non-generic)
IServiceProvider serviceProvider = ...
Type genericType = typeof(IValidator<>).MakeGenericType(typeof(Person));
IValidator validator = serviceProvider.GetService(genericType);