How To Implement Validation With MediatR And FluentValidation
Вставка
- Опубліковано 25 чер 2024
- 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:
ua-cam.com/users/MilanJovano...
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 - Наука та технологія
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/cqrs-validation
Hi Milan, is it possible to get the access to the source code?
Concept clear now, fully explained
Thanks Buddy 🥳🥳🥳keep it up
Awesome, I'm glad all the pieces made sense in the end.
Your video is awesome as always!!!
Thank you very much!
Yeeee thank you Milan!
You're welcome!
this video help me to solve problem ... thank you man
You're most welcome!
Very nice video. It helps me make my validation result much cleaner and simpler. Thank you!
Glad it was helpful!
Hi Milan, awesome video as always 🙂. Any chance you'll be doing authorization/authentication with a clean architecture setup?
It's going to happen for sure!
@@MilanJovanovicTech will you build on top of identity server and implement a decent user management?
Thank you!
You're welcome!
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 😉
It would surely make for an interesting comparison.
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.
Thanks
No problem!
Awesome ❤ do you have a vertical slice architecture lesson?
ua-cam.com/video/msjnfdeDCmo/v-deo.html
Great video! One comment about the HandleError method... shouldn't the wildcard return a 505 instead of a 404?
You can pick what you think is best for your API
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.
More or less, yes. Do you feel like something is missing?
@@MilanJovanovicTech No, it is fine, just I am concerned about the performance in a high load system.
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 👍
Nope, you got it right
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.
Different kind of validation. It's a tradeoff I can live with.
Thanks for the video! I'm not clear about a point here. How can you pass a list of validators to specific handler?
If you create multiple AbstractValidator for the same command, they'll be passed in to the behavior
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.
That would've been interesting
Please can you show it in the example how to do it?
Nice video. How do you keep the open api spec (swagger) and the fluent validation rules in sync in the daily life? Any tips?
I don't think about that, to be honest
@@MilanJovanovicTech hehe, ok. then you never had team-external consumers 🤪
@@pwn2own23 there is a nuget for that called MicroElements.Swashbuckle.FluentValidation which adds all your FluentValidation validators to the swagger schema.
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
I share it on my Patreon
Can you please make a video on logging using Serilog in your setup?
Sure, Serilog is a great library
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?
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.
Please create a video on micro services and micro frontend
I'll consider it
thanks for your time but keep it simple as possible
What do you mean?
Hi Milan. Thanks a lot for sharing such a nice and helpful information. Can you please share the source code of this project?
Send me an email: milan@milanjovanovic.tech
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?
I'll see what I can do 😁
@@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!
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.
You can add a NotDefault check with FluentValidation
Do we need the problem detail object if we use the result pattern (eg. languageExtcore)
As long as you return a standardized response from the API, I would consider it alright
Hi Milan,
Why IDomainEventHandler is given in Application layer; instead of Domain layer?
That's what makes sense to me
i guess youtube was listening meeting between me and my manager who was discussing yo implement CQRS and Fluent Api
There's also this version, which is slightly different: ua-cam.com/video/OhQA4PDdp0Q/v-deo.html
Is it possible to have as pipeline like this on a crud style service?
With the decorator pattern?
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
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)
@@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"
@@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
Could you please make video on Validation Pipeline Behaviour With Minimal API?
It's the same, no?
Can we reuse this validator in blazor client? is it clean to inject or use command validators in client?
Not sure how it would work in Blazor, but you probably can
@@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?
Fluid Milan ❤
What is IValidationResult here? Does come from FluentValidation?
It comes from FluentValidation
Hi, how to return FluentValidation.Results.ValidationResult object instead of your custom validation result?
Replace it in the entire pipeline
Didn't you approach this using value objects that encapsulated this validation logic? When would you use fluent validation over value objects?
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.
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?
@@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.
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!
Hello, can you show your "Result" and "Error" classes?
You can see an example here: github.com/m-jovanovic/rally-simulator
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?
Yes
could you provide the code for result class and result class ?
gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e
@@MilanJovanovicTech Thanks 🤩
Does it still make sense to use the binding validation on the request body object that .net validate as default?
Yes, but if you're using [ApiController] or minimal APIs those will be applied by default
@@MilanJovanovicTech so both validations would be applied? aren't they doing kind the same in the end of the day?
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?
Which .NET version are you using?
I did not realize that in class Result what is it
What exactly?
Hi Milan, thanks for your video
are you going to talk about CQRS and Minimal API in Clean Architecture?
CQRS was the previous video. And I'm planning Minimal API soon :)
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
That's just one way to do it
hey man, you do not show Error Class :(
Just a simple class with Code and Description properties
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.
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.
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
There was basically zero "DDD patterns" in this video. What are you talking about?
man you are complicating things too much!! you are using the fluentValidation library, so just use it instead of extending it !
What are you talking about? With the pipeline in place, I can just create validators and they'll be applied automatically
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));
I am using .NET 8
internal sealed class ValidationPipelineBehaviour
: IPipelineBehavior
where TRequest : notnull
where TResponse : Result
here, after changed generic constraint IRequest to notnull, this pipeline behaviour get called.😲
Glad you managed to solve it