Skip to content

Instantly share code, notes, and snippets.

@kdowswell
Created April 29, 2016 21:17
Show Gist options
  • Save kdowswell/37ff28573f5468d4236e03d3f511c4b3 to your computer and use it in GitHub Desktop.
Save kdowswell/37ff28573f5468d4236e03d3f511c4b3 to your computer and use it in GitHub Desktop.
public class ValidatorHandler<TRequest, TResponse>
: IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IRequestHandler<TRequest, TResponse> _inner;
private readonly IValidator<TRequest> _validator;
public ValidatorHandler(IRequestHandler<TRequest, TResponse> inner,
IValidator<TRequest> validator)
{
_inner = inner;
_validator = validator;
}
public TResponse Handle(TRequest request)
{
var validationResult = _validator.Validate(request);
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
return _inner.Handle(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment