Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Announcement: Deprecation of Validator Factories #1961

Copy link
Copy link
@JeremySkinner

Description

@JeremySkinner
Issue body actions

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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.