PipelineBehaviour for all Requests
See original GitHub issueHi,
i’m new to MediatR and i tried to create a validation behavior for all my requests (commands and queries). But the example i’ve found works for queries only. So the question is, do i have to implement all behaviours twice?
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehaviour<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> validators;
public ValidationBehaviour( IEnumerable<IValidator<TRequest>> validators )
{
if (validators == null)
{
throw new ArgumentNullException(nameof(validators));
}
this.validators = validators;
}
public Task<TResponse> Handle( TRequest request, RequestHandlerDelegate<TResponse> next )
{
var context = new ValidationContext( request );
var failures = this.validators.Select( v => v.Validate( context ) ).SelectMany( result => result.Errors ).Where( f => f != null ).ToList();
if( failures.Any() )
{
throw new ValidationException( failures );
}
return next();
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Question about IPipelineBehavior only for certain types of ...
I would like to register an IPipelineBehavior in a way that it gets executed only for certain types of requests (using a marker...
Read more >MS DI + MediatR: PipelineBehavior for a base IRequest ...
I'm trying to add a pipeline behavior on a base request that validates that the invoker has access to the tenant they are...
Read more >MediatR Pipeline Behaviour in ASP.NET Core - Logging ...
This way , we will be sending only necesarry valid requests to the CQRS Implementation. Logging and Validation using this MediatR Pipeline ......
Read more >A Demo On Request Logging Using MediatR ...
Here it first executes all the 'IPipelineBehavior' implementations request logic and then executes the 'Handlers' and then again executes ...
Read more >How to use MediatR Pipeline Behaviours
One of the most common things you'll need to do when using MediatR is validation. Most likely you'd like to validate your Request...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The wiki is open for anyone to contribute.
Thanks for your help. I got it working now with the PreRequestProcessor. I found out that i forgot to register the RequestPreProcessorBehaviour. Now everything is working as expected.
I have to say that i really like your MediatR library, but it is really hard to find some documentation and examples beyond the standard. Maybe you can improve this in the future. 😃