Created
December 20, 2019 16:17
-
-
Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidatorBehavior<TRequest, TResponse> | |
: IPipelineBehavior<TRequest, TResponse> where TRequest : IValidatable | |
{ | |
private readonly IEnumerable<IValidator<T>> _validators; | |
public ValidatorBehavior(IEnumerable<IValidator<T>> validators) | |
=> _validators = validators | |
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | |
{ | |
var results = _validators.Select(v => v.Validate(request)).ToList(); | |
if (results.Any(result => result.Invalid)) { | |
throw new ValidationException(result); | |
} | |
return next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment