How To Implement Validation With MediatR And FluentValidation

  Рет қаралды 45,941

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 121
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/cqrs-validation Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@F2H16
@F2H16 Жыл бұрын
Hi Milan, is it possible to get the access to the source code?
@rakkarajput
@rakkarajput 2 жыл бұрын
Concept clear now, fully explained Thanks Buddy 🥳🥳🥳keep it up
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Awesome, I'm glad all the pieces made sense in the end.
@roefski
@roefski 2 жыл бұрын
Hi Milan, awesome video as always 🙂. Any chance you'll be doing authorization/authentication with a clean architecture setup?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
It's going to happen for sure!
@flobuilds
@flobuilds 2 жыл бұрын
@@MilanJovanovicTech will you build on top of identity server and implement a decent user management?
@ayanesuarezromero4222
@ayanesuarezromero4222 4 ай бұрын
Very nice video. It helps me make my validation result much cleaner and simpler. Thank you!
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Glad it was helpful!
@bookuha
@bookuha 2 жыл бұрын
Yeeee thank you Milan!
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
You're welcome!
@ibrahimzaky3056
@ibrahimzaky3056 3 ай бұрын
this video help me to solve problem ... thank you man
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
You're most welcome!
@AboutCleanCode
@AboutCleanCode 2 жыл бұрын
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 2 жыл бұрын
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.
@kirillhorn3186
@kirillhorn3186 2 жыл бұрын
Your video is awesome as always!!!
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Thank you very much!
@davidpccode
@davidpccode Жыл бұрын
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 Жыл бұрын
Nope, you got it right
@omar-fathy01
@omar-fathy01 9 ай бұрын
Awesome ❤ do you have a vertical slice architecture lesson?
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
kzbin.info/www/bejne/o6TNn5mamql2o9E
@sphrtehrani
@sphrtehrani Жыл бұрын
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 Жыл бұрын
Different kind of validation. It's a tradeoff I can live with.
@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 Жыл бұрын
Please can you show it in the example how to do it?
@rakkarajput
@rakkarajput 2 жыл бұрын
Please create a video on micro services and micro frontend
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I'll consider it
@aamirali8114
@aamirali8114 Жыл бұрын
i guess youtube was listening meeting between me and my manager who was discussing yo implement CQRS and Fluent Api
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
There's also this version, which is slightly different: kzbin.info/www/bejne/hZm0cmeGecmjZrM
@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
@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
@zikkrype
@zikkrype 2 жыл бұрын
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 2 жыл бұрын
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 2 жыл бұрын
@@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
@fieryscorpion
@fieryscorpion 2 жыл бұрын
Can you please make a video on logging using Serilog in your setup?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Sure, Serilog is a great library
@thomaswoods1365
@thomaswoods1365 7 ай бұрын
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 7 ай бұрын
I'll see what I can do 😁
@thomaswoods1365
@thomaswoods1365 7 ай бұрын
@@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!
@justhobbes4682
@justhobbes4682 2 жыл бұрын
Didn't you approach this using value objects that encapsulated this validation logic? When would you use fluent validation over value objects?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
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 2 жыл бұрын
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 2 жыл бұрын
@@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 2 жыл бұрын
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!
@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.
@SalmanShafiq-y3q
@SalmanShafiq-y3q 7 ай бұрын
Fluid Milan ❤ What is IValidationResult here? Does come from FluentValidation?
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
It comes from FluentValidation
@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.
@hakimedusti
@hakimedusti 4 ай бұрын
Thanks
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
No problem!
@VitaliiShmidt-UA
@VitaliiShmidt-UA Жыл бұрын
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
@mustafahaider9115
@mustafahaider9115 3 ай бұрын
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 3 ай бұрын
That's just one way to do it
@SalmanShafiq-y3q
@SalmanShafiq-y3q 7 ай бұрын
Could you please make video on Validation Pipeline Behaviour With Minimal API?
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
It's the same, no?
@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
@ИванИванов-о8я9с
@ИванИванов-о8я9с Жыл бұрын
Hi, how to return FluentValidation.Results.ValidationResult object instead of your custom validation result?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Replace it in the entire pipeline
@Omar.bin.khattab
@Omar.bin.khattab 11 ай бұрын
thanks for your time but keep it simple as possible
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
What do you mean?
@masoudmotlagh6384
@masoudmotlagh6384 8 ай бұрын
Can we reuse this validator in blazor client? is it clean to inject or use command validators in client?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
Not sure how it would work in Blazor, but you probably can
@masoudmotlagh6384
@masoudmotlagh6384 8 ай бұрын
@@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?
@YoussefAbdullah-s6y
@YoussefAbdullah-s6y 8 ай бұрын
could you provide the code for result class and result class ?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e
@YoussefAbdullah-s6y
@YoussefAbdullah-s6y 6 ай бұрын
@@MilanJovanovicTech Thanks 🤩
@ПетрДюкин-ю5в
@ПетрДюкин-ю5в Жыл бұрын
Hello, can you show your "Result" and "Error" classes?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can see an example here: github.com/m-jovanovic/rally-simulator
@timur2887
@timur2887 5 ай бұрын
Thank you!
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
You're welcome!
@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
@harryh212
@harryh212 Жыл бұрын
Is it possible to have as pipeline like this on a crud style service?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
With the decorator pattern?
@awmy3109
@awmy3109 2 жыл бұрын
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 2 жыл бұрын
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.
@pwn2own23
@pwn2own23 2 жыл бұрын
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 2 жыл бұрын
I don't think about that, to be honest
@pwn2own23
@pwn2own23 2 жыл бұрын
@@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.
@ravindranaths513
@ravindranaths513 Жыл бұрын
Hi Milan, Why IDomainEventHandler is given in Application layer; instead of Domain layer?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
That's what makes sense to me
@TheEmqe
@TheEmqe 3 ай бұрын
the question is how can we show the errors on bootstrap modal ? and my project not API .net core mvc
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Return them through the API response
@syedjunaid7846
@syedjunaid7846 9 ай бұрын
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 9 ай бұрын
Yes
@verbroez712
@verbroez712 Ай бұрын
Exceptions slow down applications
@norelayaien9888
@norelayaien9888 2 жыл бұрын
I did not realize that in class Result what is it
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
What exactly?
@giovannis6506
@giovannis6506 8 ай бұрын
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 8 ай бұрын
Which .NET version are you using?
@abhikarne
@abhikarne Жыл бұрын
hey man, you do not show Error Class :(
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Just a simple class with Code and Description properties
@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
@CommentingOnTheFreakshow
@CommentingOnTheFreakshow 8 күн бұрын
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!
@MilanJovanovicTech
@MilanJovanovicTech 7 күн бұрын
We could
@Tamer_Ali
@Tamer_Ali 2 жыл бұрын
Hi Milan, thanks for your video are you going to talk about CQRS and Minimal API in Clean Architecture?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
CQRS was the previous video. And I'm planning Minimal API soon :)
@DanKac182
@DanKac182 4 ай бұрын
Does it still make sense to use the binding validation on the request body object that .net validate as default?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Yes, but if you're using [ApiController] or minimal APIs those will be applied by default
@DanKac182
@DanKac182 4 ай бұрын
@@MilanJovanovicTech so both validations would be applied? aren't they doing kind the same in the end of the day?
@pablofonseca8867
@pablofonseca8867 6 ай бұрын
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 6 ай бұрын
There was basically zero "DDD patterns" in this video. What are you talking about?
@muhamotto2084
@muhamotto2084 10 ай бұрын
man you are complicating things too much!! you are using the fluentValidation library, so just use it instead of extending it !
@MilanJovanovicTech
@MilanJovanovicTech 10 ай бұрын
What are you talking about? With the pipeline in place, I can just create validators and they'll be applied automatically
@SalmanShafiq-y3q
@SalmanShafiq-y3q 7 ай бұрын
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));
@SalmanShafiq-y3q
@SalmanShafiq-y3q 7 ай бұрын
I am using .NET 8
@SalmanShafiq-y3q
@SalmanShafiq-y3q 7 ай бұрын
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 7 ай бұрын
Glad you managed to solve it
Get Rid of Exceptions in Your Code With the Result Pattern
13:06
Milan Jovanović
Рет қаралды 51 М.
CQRS Doesn't Have To Be Complicated | Clean Architecture, .NET 6
24:09
Milan Jovanović
Рет қаралды 111 М.
Как подписать? 😂 #shorts
00:10
Денис Кукояка
Рет қаралды 8 МЛН
Когда отец одевает ребёнка @JaySharon
00:16
История одного вокалиста
Рет қаралды 6 МЛН
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
Make Your Business Rules Cleaner With Fluent Validation
15:14
Milan Jovanović
Рет қаралды 29 М.
.NET 7  💥  - Intro to CQRS and MediatR with ASP.NET Core Web Api
1:06:48
The Problem With Microservices
17:47
Continuous Delivery
Рет қаралды 434 М.
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 72 М.
Clean ASP.NET Core API using MediatR and CQRS | Setup
22:39
Nick Chapsas
Рет қаралды 323 М.
Как подписать? 😂 #shorts
00:10
Денис Кукояка
Рет қаралды 8 МЛН