Validator Behavior
Validator Behavior in MediatR pipeline within RCommon.
Implementation
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : class, IAppRequest<TResponse>
{
private readonly IValidationService _validationService;
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
public ValidatorBehavior(IValidationService validationService, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
{
_validationService = validationService;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var typeName = request.GetGenericTypeName();
_logger.LogInformation("----- Validating command {CommandType}", typeName);
await _validationService.ValidateAsync<TRequest>(request, true, cancellationToken);
return await next();
}
}Usage
Last updated