Last active
April 3, 2024 01:56
-
-
Save jbogard/473abe269278a3f8d9b55b8913f39d3e 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 MediatorPipeline<TRequest, TResponse> | |
: IRequestHandler<TRequest, TResponse> | |
where TRequest : IRequest<TResponse> | |
{ | |
private readonly IRequestHandler<TRequest, TResponse> _inner; | |
private readonly IEnumearble<IMessageValidator<TRequest>> _validators; | |
public MediatorPipeline(IRequestHandler<TRequest, TResponse> inner, | |
IEnumerable<IMessageValidator<TRequest>> validators) | |
{ | |
_inner = inner; | |
_validators = validators; | |
} | |
public TResponse Handle(TRequest message) | |
{ | |
using (LogContext.PushProperty(LogConstants.MediatRRequestType, requestType)) | |
using (Metrics.Time(Timers.MediatRRequest)) | |
{ | |
var failuers = _validators | |
.Select(v => v.Validate(message)) | |
.SelectMany(result => result.Errors) | |
.Where(f => f != null) | |
.ToList(); | |
if (failures.Any()) | |
throw new ValidationException(failures); | |
return _inner.Handle(request); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment