I'm using FluentValidation.AspNetCore library (Version="10.3.3") for a .Net Core project.
To register validators i use this:
services.AddValidatorsFromAssemblyContaining<TestValidator>();
For example i have class called Test and i can inject validator for this class like this:
public class Service : IService
{
private readonly IValidator<Test> _testValidator;
public Service(IValidator<Test> testValidator)
{
_testValidator = testValidator;
}
}
But i dont know how to inject all registered validators into my Service constructor as IEnumerable instance. So what is the correct way to do it?
IEnumerable<IValidator<Test>>?IEnumerable<IValidator<object>>and have that list contain all registered validators. That's impossible due to the 'limitations' of the .NET's type system. This has to do with how variance and contra-variance works. Instead of injecting all validators, you can instead try to inject some sort of mediator implementation that resolves the required validators on the fly from theIServiceProvider.