简体   繁体   中英

One of the MediatR Validator is not getting registered in net6.0

I have two Validators for MediatR

public class GamerDtoValidator : AbstractValidator<GamerDto>
{
    public GamerDtoValidator()
    {
        RuleFor(v => v.Username).NotEmpty().MaximumLength(50)
            .WithMessage("Usernmae Name is required");
    }
}

and

public class CreateGamerCommandValidator : AbstractValidator<CreateGamerCommand>
{
    private readonly IGamerRepository _gamerRepository;

    public CreateGamerCommandValidator(IGamerRepository gamerRepository)
    {
        _gamerRepository = gamerRepository;
        
        RuleFor(v => v.Username).Must((gamer, cancellation) =>
        {
            var gamerExists =  _gamerRepository.AnyAsync(y => y.Username == gamer.Username).ConfigureAwait(false).GetAwaiter().GetResult();
            return !gamerExists;
        }).WithMessage("User already exist - {PropertyValue}");
    }
}

I have registered these like mentioned below

        services.AddMediatR(typeof(Program));

        services.Scan(s => s
                        .FromAssembliesOf(this.AssemblyMarkerTypes)
                        .AddClasses(false)
                        .UsingRegistrationStrategy(RegistrationStrategy.Append)
                        .AsImplementedInterfaces()
                        .WithTransientLifetime());

ValidatorBehavior Middleware is defined like below

public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> logger;
    private readonly IEnumerable<IValidator<TRequest>> validators;

    public ValidatorBehavior(IEnumerable<IValidator<TRequest>> validators, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
    {
        this.validators = validators;
        this.logger = logger;
    }

The validator "CreateGamerCommandValidator" only gets registered and applied

在此处输入图像描述

The validator "GamerDtoValidator" is ignored. Why? How to fix this so that both the validators will be registered and applied?

The provided screenshot does not prove "The validator "CreateGamerCommandValidator" only gets registered" statement. You are resolving IEnumerable<IValidator<TRequest>> validators where TRequest seems to be CreateGamerCommand so out of two provided validators only one implements IValidator<CreateGamerCommand> (transitively from AbstractValidator<CreateGamerCommand> ).

GamerDtoValidator is AbstractValidator<GamerDto> so it can't be registered as IValidator<CreateGamerCommand> and automatically is registered as IValidator<GamerDto> .

Also both should be registered as base IValidator interface, so you can check that by resolving IEnumerable<IValidator> in your ValidatorBehavior but those will not be very useful.

As for how to apply GamerDto validator - it hugely depends on what you are trying to achieve and how you application is structured. For example if for some reason CreateGamerCommand inherits from GamerDto you can resolve GamerDtoValidator there and use it. Or you can introduce an interface IHaveGamerName (with Username property) and implement it in both CreateGamerCommand and GamerDto and create a validator for IHaveGamerName and resolve it where appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM