Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Announcement: Deprecation of Validator Factories #1961

Closed
JeremySkinner opened this issue Jun 18, 2022 · 0 comments
Closed

Announcement: Deprecation of Validator Factories #1961

JeremySkinner opened this issue Jun 18, 2022 · 0 comments

Comments

@JeremySkinner
Copy link
Member

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);
@FluentValidation FluentValidation locked as resolved and limited conversation to collaborators Jun 18, 2022
@JeremySkinner JeremySkinner pinned this issue Jun 18, 2022
@JeremySkinner JeremySkinner unpinned this issue Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant