Skip to content

Instantly share code, notes, and snippets.

@jbogard
Created December 20, 2019 16:17
Show Gist options
  • Select an option

  • Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.

Select an option

Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.

Revisions

  1. jbogard created this gist Dec 20, 2019.
    18 changes: 18 additions & 0 deletions GenericConstraints.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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();
    }
    }