How To Implement Validation With MediatR And FluentValidation

  Рет қаралды 42,407

Milan Jovanović

Milan Jovanović

Күн бұрын

Get the source code for this video for FREE → the-dotnet-weekly.ck.page/cqr...
☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
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:
www.milanjovanovic.tech
Subscribe for more:
kzbin.info...
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

Пікірлер: 118
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/cqrs-validation
@F2H16
@F2H16 Жыл бұрын
Hi Milan, is it possible to get the access to the source code?
@rakkarajput
@rakkarajput Жыл бұрын
Concept clear now, fully explained Thanks Buddy 🥳🥳🥳keep it up
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Awesome, I'm glad all the pieces made sense in the end.
@kirillhorn3186
@kirillhorn3186 Жыл бұрын
Your video is awesome as always!!!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much!
@ibrahimzaky3056
@ibrahimzaky3056 26 күн бұрын
this video help me to solve problem ... thank you man
@MilanJovanovicTech
@MilanJovanovicTech 25 күн бұрын
You're most welcome!
@bookuha
@bookuha Жыл бұрын
Yeeee thank you Milan!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're welcome!
@ayanesuarezromero4222
@ayanesuarezromero4222 2 ай бұрын
Very nice video. It helps me make my validation result much cleaner and simpler. Thank you!
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
Glad it was helpful!
@timur2887
@timur2887 2 ай бұрын
Thank you!
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
You're welcome!
@user-ni7ce4fe4k
@user-ni7ce4fe4k Ай бұрын
Thanks
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
No problem!
@AboutCleanCode
@AboutCleanCode Жыл бұрын
I see your design growing and growing - in these sense of number of classes involved - with the decoupled design you have chosen. would be interesting to compare it to an alternative, more pragmatic design where input validation just happens in the controller and the application logic is just called via API from there as well 😉
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It would surely make for an interesting comparison.
@elpe21
@elpe21 Жыл бұрын
Yeah, the validation done in this video can be done with DataAnnotations. I'm in the process of googling opinions on DataAnnotations with some class-validations vs FluentValidations.
@roefski
@roefski Жыл бұрын
Hi Milan, awesome video as always 🙂. Any chance you'll be doing authorization/authentication with a clean architecture setup?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's going to happen for sure!
@flobuilds
@flobuilds Жыл бұрын
@@MilanJovanovicTech will you build on top of identity server and implement a decent user management?
@omar-fathy01
@omar-fathy01 6 ай бұрын
Awesome ❤ do you have a vertical slice architecture lesson?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
kzbin.info/www/bejne/o6TNn5mamql2o9E
@abderrazakhenka3903
@abderrazakhenka3903 Жыл бұрын
Hi Milan, thanks for sharing knowledge, I am watching your videos, and I like what you do. I have a question, are you using same code of CreateValidationResult(Error[] errors){...} in your production code? or this is just a simple example.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
More or less, yes. Do you feel like something is missing?
@abderrazakhenka3903
@abderrazakhenka3903 Жыл бұрын
@@MilanJovanovicTech No, it is fine, just I am concerned about the performance in a high load system.
@davidpccode
@davidpccode 11 ай бұрын
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 👍
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
Nope, you got it right
@pazzuto
@pazzuto Жыл бұрын
Great video! One comment about the HandleError method... shouldn't the wildcard return a 505 instead of a 404?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can pick what you think is best for your API
@Omar.bin.khattab
@Omar.bin.khattab 9 ай бұрын
thanks for your time but keep it simple as possible
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
What do you mean?
@dirceusjr
@dirceusjr Жыл бұрын
The validation result creation could be simplified by creating an interface TResult with a SetError() method. With that interface on your result type, you just need to call new TResult() and then the SetError() method in the ValidationBehaviour.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
That would've been interesting
@davittonoyan6537
@davittonoyan6537 11 ай бұрын
Please can you show it in the example how to do it?
@sphrtehrani
@sphrtehrani 11 ай бұрын
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.
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
Different kind of validation. It's a tradeoff I can live with.
@aamirali8114
@aamirali8114 10 ай бұрын
i guess youtube was listening meeting between me and my manager who was discussing yo implement CQRS and Fluent Api
@MilanJovanovicTech
@MilanJovanovicTech 10 ай бұрын
There's also this version, which is slightly different: kzbin.info/www/bejne/hZm0cmeGecmjZrM
@empireofhearts
@empireofhearts Жыл бұрын
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
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I share it on my Patreon
@codearabawy
@codearabawy Жыл бұрын
Thanks for the video! I'm not clear about a point here. How can you pass a list of validators to specific handler?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
If you create multiple AbstractValidator for the same command, they'll be passed in to the behavior
@pwn2own23
@pwn2own23 Жыл бұрын
Nice video. How do you keep the open api spec (swagger) and the fluent validation rules in sync in the daily life? Any tips?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I don't think about that, to be honest
@pwn2own23
@pwn2own23 Жыл бұрын
@@MilanJovanovicTech hehe, ok. then you never had team-external consumers 🤪
@PelFox
@PelFox Жыл бұрын
@@pwn2own23 there is a nuget for that called MicroElements.Swashbuckle.FluentValidation which adds all your FluentValidation validators to the swagger schema.
@rakkarajput
@rakkarajput Жыл бұрын
Please create a video on micro services and micro frontend
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I'll consider it
@fieryscorpion
@fieryscorpion Жыл бұрын
Can you please make a video on logging using Serilog in your setup?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Sure, Serilog is a great library
@vitaliishmidt3704
@vitaliishmidt3704 Жыл бұрын
Hi Milan. Thanks a lot for sharing such a nice and helpful information. Can you please share the source code of this project?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Send me an email: milan@milanjovanovic.tech
@thomaswoods1365
@thomaswoods1365 4 ай бұрын
Great video Milan, could you possibly show how this could be achieved using FastEndpoints implementation? How would someone setup a base class like you did at 15:21 with HandleFailure in a FastEndpoints solution?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
I'll see what I can do 😁
@thomaswoods1365
@thomaswoods1365 4 ай бұрын
@@MilanJovanovicTech the solution for validation in FastEndpoints incorporates FluentValidation. I'll follow their guidance on that. Take a look. If you have any improvements I'd love to see it. Thank you Milan!
@viktorasmickunas2527
@viktorasmickunas2527 Жыл бұрын
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?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I like to customize it per the project needs. I started using this a while ago. I wasn't aware of FluentResults at the time, so I just stuck with mine. I'll see what it offers past the basics.
@r75shell
@r75shell Жыл бұрын
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.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can add a NotDefault check with FluentValidation
@singernooneheard6967
@singernooneheard6967 Жыл бұрын
Do we need the problem detail object if we use the result pattern (eg. languageExtcore)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
As long as you return a standardized response from the API, I would consider it alright
@user-gn7bd6mq8n
@user-gn7bd6mq8n 4 ай бұрын
Could you please make video on Validation Pipeline Behaviour With Minimal API?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
It's the same, no?
@ravindranaths513
@ravindranaths513 11 ай бұрын
Hi Milan, Why IDomainEventHandler is given in Application layer; instead of Domain layer?
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
That's what makes sense to me
@zikkrype
@zikkrype Жыл бұрын
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
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I don't like to place async validation in the pipeline. This would probably be data validations or something similar. I prefer to place these in the application layer directly (handlers)
@zikkrype
@zikkrype Жыл бұрын
@@MilanJovanovicTech like it or not, but here validation is flawed. I don't think that it's the best idea to write something like "please use Validate over ValidateAsync when writing validators"
@elpe21
@elpe21 Жыл бұрын
@@MilanJovanovicTech I agree with @Nazarii. You were already made a video about Email address being unique where you have to access a database which is perfect example. Lastly, I'm not sure if I'm fan or not of Fluent Validations. Too often you have to write own functions anyway
@harryh212
@harryh212 Жыл бұрын
Is it possible to have as pipeline like this on a crud style service?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
With the decorator pattern?
@masoudmotlagh6384
@masoudmotlagh6384 6 ай бұрын
Can we reuse this validator in blazor client? is it clean to inject or use command validators in client?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
Not sure how it would work in Blazor, but you probably can
@masoudmotlagh6384
@masoudmotlagh6384 6 ай бұрын
@@MilanJovanovicTech we can use the pipeline validator in client project but I was thinking if that is a good and clean way to mention "command" in blazor client project and use like this: ... We can add another validator for viewModels or dtos with same validation rules but then we have to maintain two validators for one scenario. so, what do yo think is the best way to reuse validators? or maybe we should just consider sharing the rules between validators?
@user-ow6gy6xj5v
@user-ow6gy6xj5v Жыл бұрын
Hi, how to return FluentValidation.Results.ValidationResult object instead of your custom validation result?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Replace it in the entire pipeline
@syedjunaid7846
@syedjunaid7846 6 ай бұрын
Alternatively, could we just throw an exception during validation, which will encompass all our errors with mabye an 'errors' property, and handle that exception in an exception handling middleware in our presentation layer?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
Yes
@user-ds9fm1sm4e
@user-ds9fm1sm4e 5 ай бұрын
could you provide the code for result class and result class ?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e
@user-ds9fm1sm4e
@user-ds9fm1sm4e 3 ай бұрын
@@MilanJovanovicTech Thanks 🤩
@Tamer_Ali
@Tamer_Ali Жыл бұрын
Hi Milan, thanks for your video are you going to talk about CQRS and Minimal API in Clean Architecture?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
CQRS was the previous video. And I'm planning Minimal API soon :)
@user-gn7bd6mq8n
@user-gn7bd6mq8n 4 ай бұрын
Fluid Milan ❤ What is IValidationResult here? Does come from FluentValidation?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
It comes from FluentValidation
@DanKac182
@DanKac182 2 ай бұрын
Does it still make sense to use the binding validation on the request body object that .net validate as default?
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
Yes, but if you're using [ApiController] or minimal APIs those will be applied by default
@DanKac182
@DanKac182 2 ай бұрын
@@MilanJovanovicTech so both validations would be applied? aren't they doing kind the same in the end of the day?
@user-rp2rt6ws8m
@user-rp2rt6ws8m Жыл бұрын
Hello, can you show your "Result" and "Error" classes?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can see an example here: github.com/m-jovanovic/rally-simulator
@giovannis6506
@giovannis6506 5 ай бұрын
For some reason my ValidationPipelineBehavior is not firing off. It appears that "where TResponse : Result" is not found by Mediatr. Any advice assuming I followed your tutorial?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Which .NET version are you using?
@justhobbes4682
@justhobbes4682 Жыл бұрын
Didn't you approach this using value objects that encapsulated this validation logic? When would you use fluent validation over value objects?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I use this approach for Input validation. Simple stuff like not null, not empty. For more complex stuff, I like to keep it in the Application/Domain layers.
@justhobbes4682
@justhobbes4682 Жыл бұрын
Does that mean you allow nulls or empty strings in your domain layer? And if not, isn’t that duplication of validation logic? Both sound a bit iffy! What do you think?
@arjavdave1015
@arjavdave1015 Жыл бұрын
@@justhobbes4682 I am also a bit confused here. If we are going for rich domain models then I don't see a reason to have fluent validation.
@stjepankarin
@stjepankarin Жыл бұрын
You need to understand the difference between input validation and business validation. Take an email address field for ex. Input validation will ensure that, given we want this field to be mandatory, it is not null/empty and it is a valid email address, conforming to the current spec. Business validation on the other hand might ensure that only specific set of email addresses are valid like from specific domain, or that some unique address within that application are allowed, etc. It is preferred to keep these separate as they by nature evolve for a different reasons. Also, input validation is always placed in most outer layer, while business is part of application or in some cases core domain. This will ensure neither empty strings nor nulls (if that is what we want) can get it to you domain layer. I hope this helps with clarification!
@norelayaien9888
@norelayaien9888 Жыл бұрын
I did not realize that in class Result what is it
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
What exactly?
@TheEmqe
@TheEmqe 17 күн бұрын
the question is how can we show the errors on bootstrap modal ? and my project not API .net core mvc
@MilanJovanovicTech
@MilanJovanovicTech 16 күн бұрын
Return them through the API response
@mustafahaider9115
@mustafahaider9115 Ай бұрын
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
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
That's just one way to do it
@abhikarne
@abhikarne Жыл бұрын
hey man, you do not show Error Class :(
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Just a simple class with Code and Description properties
@awmy3109
@awmy3109 Жыл бұрын
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.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's a powerful package when you look beyond method calls. PipelineBehaviors is my favorite feature. Each handler class is simple, and ideally does one thing, which promotes single responsibility.
@pablofonseca8867
@pablofonseca8867 3 ай бұрын
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
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
There was basically zero "DDD patterns" in this video. What are you talking about?
@muhamotto2084
@muhamotto2084 7 ай бұрын
man you are complicating things too much!! you are using the fluentValidation library, so just use it instead of extending it !
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
What are you talking about? With the pipeline in place, I can just create validators and they'll be applied automatically
@user-gn7bd6mq8n
@user-gn7bd6mq8n 4 ай бұрын
Validation Pipeline not call in mediator Pipeline bahaviour. Initially I added this pipeline like this way. services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), includeInternalTypes: true); services.AddMediatR(cfg => { cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); cfg.AddOpenBehavior(typeof(RequestLoggingBehaviour)); cfg.AddOpenBehavior(typeof(ValidationPipelineBehaviour)); }); after not working then i did like this way. services.AddScoped(typeof(IPipelineBehavior), typeof(ValidationPipelineBehaviour));
@user-gn7bd6mq8n
@user-gn7bd6mq8n 4 ай бұрын
I am using .NET 8
@user-gn7bd6mq8n
@user-gn7bd6mq8n 4 ай бұрын
internal sealed class ValidationPipelineBehaviour : IPipelineBehavior where TRequest : notnull where TResponse : Result here, after changed generic constraint IRequest to notnull, this pipeline behaviour get called.😲
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Glad you managed to solve it
Get Rid of Exceptions in Your Code With the Result Pattern
13:06
Milan Jovanović
Рет қаралды 45 М.
Getting Started with Event Sourcing in .NET
37:07
Nick Chapsas
Рет қаралды 51 М.
Happy 4th of July 😂
00:12
Pink Shirt Girl
Рет қаралды 61 МЛН
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 147 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
MediatR Validation and the Chain of Responsibility Pattern
10:22
Clean ASP.NET Core API using MediatR and CQRS | Setup
22:39
Nick Chapsas
Рет қаралды 319 М.
Make Your Business Rules Cleaner With Fluent Validation
15:14
Milan Jovanović
Рет қаралды 26 М.
Modular Monoliths Are The New Microservices
31:08
TaleLearnCode
Рет қаралды 23 М.
.NET 7  💥  - Intro to CQRS and MediatR with ASP.NET Core Web Api
1:06:48
Как распознать поддельный iPhone
0:44
PEREKUPILO
Рет қаралды 1,6 МЛН
😱Хакер взломал зашифрованный ноутбук.
0:54
Последний Оплот Безопасности
Рет қаралды 655 М.
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 7 МЛН
АЙФОН 20 С ФУНКЦИЕЙ ВИДЕНИЯ ОГНЯ
0:59
КиноХост
Рет қаралды 410 М.
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 22 МЛН