How To Implement Validation With MediatR And FluentValidation

preview_player
Показать описание

In this video, I'll show how you can elegantly introduce validation when implementing the CQRS design pattern. We will use MediatR's BehaviorPipeline feature and FluentValidaton for implementing the validation. CQRS gives us a lot of flexibility to introduce additional behavior in the request pipeline.

Join my weekly .NET newsletter:

Subscribe for more:

Chapters
0:00 Validation Result
1:37 Creating the ValidationPipelineBehavior
3:38 Implementing validation with FluentValidation
6:44 Create ValidationResult with reflection
10:30 Creating a command validator with AbstractValidator
12:02 Configuring everything with DI
13:11 Testing our ValidationPipelineBehavior
14:33 Returning errors as Problem Details response
17:28 Alternative approach to returning Validation Result
Рекомендации по теме
Комментарии
Автор

Very nice video. It helps me make my validation result much cleaner and simpler. Thank you!

ayanesuarezromero
Автор

Hi Milan, awesome video as always 🙂. Any chance you'll be doing authorization/authentication with a clean architecture setup?

roefski
Автор

i guess youtube was listening meeting between me and my manager who was discussing yo implement CQRS and Fluent Api

aamirali
Автор

I honestly don't know why C# programmers keep pushing this MediatR package. I really don't see it's usefulness. Simple methods or classes that take required interfaces as arguements is a simple, straight forward solution. If you like, you can make them static and not need to instantiate anything to use them.

awmy
Автор

Hello great content. Thank you very much. I have read and seen a lot of content about command validation but I still can't find a reason that justifies so much over-engineering and so much complexity.
I can achieve the same thing by injecting a class that performs all the validations with less than half the effort and with the same result. what am i missing? Thanks in advance 👍

davidpccode
Автор

man you are complicating things too much!! you are using the fluentValidation library, so just use it instead of extending it !

muhamotto
Автор

How do you handle class fields binding errors? like if command has integer fields, and someone passed "one" which is not a number. Isn't it converted into 0 by default? And, therefore going through all code, because we don't check ModelState.IsValid.

rshell
Автор

Hi, how to return object instead of your custom validation result?

ИванИванов-ояс
Автор

Is there a specific reason why you create Result/Error, etc. classes yourself? There is a FluentResults package that seems to provide a reasonable implementation for this. Have you tried it?

viktorasmickunas
Автор

OR, you could just create simple object with functions that perform your validation where you pass in the data object and just do your validation checks directly on the model object and save yourself all that needless abstraction which is not free in terms of application performance!

CommentingOnTheFreakshow
Автор

Hi Milan. Great videos. Now we are validating our entities twice. Once in pipeline behaviour with fluent validation and once in our value objects. Isn't it code duplication.

sphrtehrani
Автор

Fluid Milan ❤
What is IValidationResult here? Does come from FluentValidation?

SalmanShafiq-yq
Автор

Thanks for the video! I'm not clear about a point here. How can you pass a list of validators to specific handler?

codearabawy
Автор

the only thing that seems to be strange for me is you only register the service of the validationPipeLine you didn't actually use it as a mediatr middleware so how did it get used, it is so strange to me I use the same technique but I use the addBehavior

but anyway thank you for all the efforts

mustafahaider
Автор

is there repo for the project you uses in these videos? it would be really helpful to have all in one place to go thorugh. @milan

empireofhearts
Автор

Nice. But there are plenty of those over the internet with such fluent validation usage in mediatr pipeline. But none of those considered that AbstractValidator has also async overload. And your validator may have Validate or ValidateAsync implemented or even both. And in this case ValidateAsync would be ignored. Release Part II with such improvement. And I guess you will be the first one

zikkrype
Автор

Didn't you approach this using value objects that encapsulated this validation logic? When would you use fluent validation over value objects?

justhobbes
Автор

Great video! One comment about the HandleError method... shouldn't the wildcard return a 505 instead of a 404?

pazzuto
Автор

Seems like everyone is addicted to DDD pattern because they want to be popular, code structure is complicated to read and understand as F, but they apply it to be more understandable lol

pablofonseca
Автор

Awesome ❤ do you have a vertical slice architecture lesson?

omar-fathy