Skip to content

Instantly share code, notes, and snippets.

@jbogard
Created December 20, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.
Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.
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