question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PipelineBehaviour for all Requests

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, Mar 1, 2017

The wiki is open for anyone to contribute.

0reactions
endeffectscommented, Mar 1, 2017

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. 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found