No video

Validation using MediatR's Pipeline Behaviors and FluentValidation | Clean .NET Core

  Рет қаралды 88,208

Nick Chapsas

Nick Chapsas

Күн бұрын

Become a Patreon and get source code access: / nickchapsas
Check out my courses: dometrain.com
Hello everybody I'm Nick and in this ASP.NET Core tutorial I will show you how you can use MediatR's Pipeline Behavior feature to add domain level validation in your API in a very clean way. We will add it using a very intuitive way, without mixing it with our happy path logic in our main IRequestHandler. I will also explain how exactly Pipeline Behaviors work and why they are a perfect fit for this piece of functionality. The validation logic will use FluentValidation.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasG...
Follow me on Twitter: bit.ly/ChapsasT...
Connect on LinkedIn: bit.ly/ChapsasL...
#aspnetcore #mediatr #tutorial

Пікірлер: 102
@toastybowl
@toastybowl 3 жыл бұрын
@13:53 "...& Keep in mind this is domain-level validation!.." Been wondering how to do this simply with fluent validator for a while & it turns out Mediatr was the this missing link all along. Great point + easy to miss for those used to only this endpoint-level/DTO validator usage which is far more common in experience
@zabustifu
@zabustifu Жыл бұрын
DTO validation makes sense to me, considering validating domain models means your API would be returning bad requests for a different model than what was sent to your API. In the video here, it's simple because there are just two properties in the request models, but if your DTOs don't match the domain models, then your bad requests will be confusing for your API callers. In theory, it might make sense to have two validation logics (DTO validation + domain model validation), but in practice, that sounds really overkill unless you've got a very complex solution.
@davidadler8628
@davidadler8628 3 жыл бұрын
Thanks for the great video Nick. In the ValidationBehavior class, you mentioned that you were just throwing the new ValidationException(failures) exception for demonstration simplicity and that you would want to use an error handler pipeline rather than exception in production code. Can you point me to an example of the error handler pipeline?
@idan5323
@idan5323 2 жыл бұрын
I would like to know as well, I know there is an option to validate manually via a Result request object, but it would be nice to know if there is a method with less boiler plate and same performance. The only way I can think of is the Asp.Net filters method, but that is not related to mediatr.
@jamesbarlow6523
@jamesbarlow6523 4 жыл бұрын
Great video Nick! In a future video it would be really nice to see how to properly apply testing when using MediatR and the CQRS pattern.
@jasonleandro412
@jasonleandro412 3 жыл бұрын
you all probably dont care at all but does someone know a tool to get back into an Instagram account..? I stupidly forgot my login password. I appreciate any tips you can give me.
@abdullahdanny9420
@abdullahdanny9420 3 жыл бұрын
@Jason Leandro instablaster ;)
@jasonleandro412
@jasonleandro412 3 жыл бұрын
@Abdullah Danny thanks so much for your reply. I found the site through google and im waiting for the hacking stuff now. Looks like it's gonna take a while so I will get back to you later with my results.
@jasonleandro412
@jasonleandro412 3 жыл бұрын
@Abdullah Danny It worked and I finally got access to my account again. Im so happy:D Thanks so much you saved my account !
@abdullahdanny9420
@abdullahdanny9420 3 жыл бұрын
@Jason Leandro Happy to help =)
@emmanuelcaamal8201
@emmanuelcaamal8201 3 жыл бұрын
Thanks for the demostration, is a exelent form to initialize with MediatR and CQRS on create API services
@RamiNassarPlus
@RamiNassarPlus 4 жыл бұрын
Thanks for your dedication to what you do. Will you consider using a zoom in tool whenever you start coding 🙂
@shreyasjejurkar1233
@shreyasjejurkar1233 4 жыл бұрын
Nice. Looking forward to implementing notifications in the Meditr pattern and using it in project about success or failure operation.
@gerappa1126
@gerappa1126 3 жыл бұрын
Brilliant work, your videos are precious and clear. Like it!
@TBPer
@TBPer 4 жыл бұрын
Awesome video. Well-explained with perfect pace!
@benjamincharlton3774
@benjamincharlton3774 3 жыл бұрын
I love these videos. I'm learning so much about modern patterns and practices after a long break from professional programming. Thank you! I tried to adapt the pattern in this video so as not to throw a validation exception (as you recommended yourself) but I found it to be very difficult and didn't find any good examples on the internet. Is there a pattern you recommend? Do you still believe it's important not to throw a costly ValidationException to signal an invalid command?
@mohammadjaber3898
@mohammadjaber3898 Жыл бұрын
i returned a solid state result contains my result info instead of throwing exceptions Result : is my Return Result Class contains a method named Feiler to hold the error information this code is not optimized yet but i hope it the startup answer for not using exceptions if (failures.Any()) { var errors = failures.Select(p => p.ErrorMessage).ToArray(); if(typeof(TResponse).Name.Contains("Result")) { MethodInfo builderMethod = typeof(TResponse).GetMethod("Failure", BindingFlags.Static | BindingFlags.Public); var res = (TResponse)builderMethod.Invoke(null, new object[] { errors }); return res; } }
@MrAbidakhtar
@MrAbidakhtar 4 жыл бұрын
Fabulous Nick Just loved it a lot
@leandrowitzke6405
@leandrowitzke6405 4 жыл бұрын
Amazing video, very clearly. Thanks for sharing
@Fred-yq3fs
@Fred-yq3fs 3 жыл бұрын
At around 10:45: Instead of throwing exceptions from validation, return a Result Then in the pipeline translate it to a BadRequest, etc... Which pipeline? An action filter in the API I guess?
@rezayegane7708
@rezayegane7708 2 жыл бұрын
@Elfocrash please answer thsi question.
@SajadJalilian
@SajadJalilian 3 жыл бұрын
If you have a playlist, please add it you your video descriptions. It's always hard for me to find a playlist which video is in it
@sairk6174
@sairk6174 4 жыл бұрын
Great Nick !!
@yaraslauhadunou547
@yaraslauhadunou547 4 жыл бұрын
Cool! Thank you Nick!
@lpsoldier357
@lpsoldier357 4 жыл бұрын
Awesome content. Thanks!
@clarke8226
@clarke8226 9 ай бұрын
Thanks for the great content and videos, by the way... how to do unit tests for this pipeline validation behavior?
@techdeceptive
@techdeceptive 2 жыл бұрын
Thank you Boss!
@davidprice350
@davidprice350 4 жыл бұрын
As you mentioned Domain level validation; would you implement a validator for say "User does not exist" during a login process? If not, how would you implement this at the Handler level?
@F2H16
@F2H16 4 жыл бұрын
It's a great video.Thanks a lot.
@chiragpatel4601
@chiragpatel4601 2 жыл бұрын
how to adjust pipeline Behavior sequence. for ex. request-> Validate-> Cache-> Response and request->Cache->Validate->Response.
@nexaroth
@nexaroth 2 жыл бұрын
I need an answer on this question as well!
@talkathiriify
@talkathiriify 4 жыл бұрын
Very Awesome Thank you very much.
@avanthasiriwardana
@avanthasiriwardana 2 жыл бұрын
Subscribed pal :)
@temp50
@temp50 3 жыл бұрын
And is it a good thing to let the raw request data to hit the action method? Every developer in the team should be aware of this behavior, otherwise one could think that the request data has already been validated.
@moonbiscuit8742
@moonbiscuit8742 4 жыл бұрын
cool and clean
@balakrishnang5603
@balakrishnang5603 3 жыл бұрын
good job
@ankitnagpal5733
@ankitnagpal5733 2 жыл бұрын
Thanks for the video. How does multiple behaviours share data in MediatR?
@S4UR4BH4
@S4UR4BH4 Жыл бұрын
Just one question, does adding this transient service of pipeline behavior cause significant load w.r.t to performance or it more or less same? or to be simple does this overall process of validation impact performance of the microservice?
@LemmensSteven
@LemmensSteven 4 жыл бұрын
Great video. Thanks for the explanation. What tool do you use to draw on the screen like that?
@krzysztofurban4561
@krzysztofurban4561 4 жыл бұрын
It's probably MS Whiteboard :)
@johnnybravohonk6964
@johnnybravohonk6964 3 жыл бұрын
I think the video would benefit from a word of explanation why would we use this instead of ASP.NET middlewares - just to avoid people jumping into MediatR to get this feature, not realizing this pattern is already available.
@nickchapsas
@nickchapsas 3 жыл бұрын
I think I mention that in the video but that was a while ago so I could be wrong. Middleware will validate your API level model. You shouldn’t really use them for domain related concerns. Middleware should catch things related to the api request for example “this field is missing from the body or this should be a string”. Pipelines sea with the domain model and validate domain related concerns. In reality you need both.
@corruptmind7
@corruptmind7 3 жыл бұрын
@@nickchapsas Thanks for the videos they are awesome, I am little confused on this video, Should domain level cross cutting concerns reside in domain layer or at application layer ?Domain layer exceptions (not errors) can be handled by giving a proper messge to consumer (Bad Request), however for application level or persistance level errors i want to handle in a different way(let;s say common error message and logging ), Should'nt a business level dependency be a part of the domain layer ?
@DaveRogersEsq
@DaveRogersEsq 2 жыл бұрын
Why would you use middlewares? You would end up adding loads of Middlewares for all kinds of domain-centric scenarios for every single request. So, for any one request, almost all of those middlewares would not be relevant. This is not a good approach if you care about performance. (This is not a good approach anyway 😁)
@starktony1587
@starktony1587 3 жыл бұрын
How to do unit testing for this?
@reachvkvlogs
@reachvkvlogs 4 жыл бұрын
Great video but I can't access the source code either on github
@HarjinderSingh-df7bs
@HarjinderSingh-df7bs Жыл бұрын
Hey Nick, Thanks for the great video. 10:45 I am unable to implement this. I have searched the internet and everyone is throwing exceptions. Could you please share an example of this. Thanks in advance.
@bonaoenchelcha
@bonaoenchelcha 3 жыл бұрын
Ok, let say that I'm not going to use Exception Handler, what can be a real alternative in order to say something went wrong?
@adhivenkatesh3431
@adhivenkatesh3431 3 жыл бұрын
Hi your videos are awesome , may I know the editor your using for the dotnet core
3 жыл бұрын
Rider from JetBrains
@adhivenkatesh3431
@adhivenkatesh3431 3 жыл бұрын
@ , thanks for your reply , just found a link for the comparison www.jetbrains.com/rider/compare/rider-vs-visual-studio/
@somnathroy4875
@somnathroy4875 3 жыл бұрын
Are these codes still available in github? I looked for it but couldn't find it. Really loved the videos.
@nickchapsas
@nickchapsas 3 жыл бұрын
Hello Somnath. The code is available to my Patreons
@hanspetervollhorst1
@hanspetervollhorst1 2 жыл бұрын
Isn't it a bit nit-picky to consider the performance impact of one throw-catch per request in the context of network communication?
@nickchapsas
@nickchapsas 2 жыл бұрын
Depends on your performance requirements
@hanspetervollhorst1
@hanspetervollhorst1 2 жыл бұрын
@@nickchapsas Can you make a course/ video on Validation, especially focussing on which responsibility lies in which levels of validation (Domain, Api)? I checked your video on CleanArchitecture and ValueObjects but I am a bit befuddled by that topic now. Thank you for reading :-)
@sohamde
@sohamde Жыл бұрын
Please read this ! Or pin this In Validation Behavior.cs file Change the Var context = new ValidationContext(request) ❎❎❎❌ To Var context = new ValidationContext(request). ✔️✔️✔️ And also remove the using validation context at line 8. This is because fluent api change
@patrickhitch
@patrickhitch 3 жыл бұрын
Is it just me or did I miss where you are translating that validation exception into a response? There is nothing in your controller to handle that exception! And no attribute on the action method either
@cicerofoscarini8890
@cicerofoscarini8890 2 жыл бұрын
Hi Thank you very much for the video. I have two questions: which IDE are you using and I could not find a code on github. I'm not sure which repository to clone. Could you share it here, please? Thank you very much.
@herve952
@herve952 2 жыл бұрын
He's using Jetbrains' Rider IDE
@pedroferreira9234
@pedroferreira9234 4 жыл бұрын
I think creating a request object just to map to a command is unnecessary (4:35), since the command is already disconnected from the model. What is the advantage by creating a request object in this case?
@nickchapsas
@nickchapsas 4 жыл бұрын
The advantage is that there is a clear separation between your API layer and your domain layer. I you pass the same object then your API and your domain are coupled and you are risking making breaking changes on the API level.
@pedroferreira9234
@pedroferreira9234 4 жыл бұрын
@@nickchapsas In my project i pass to the API a "CreateValueCommand" that is different from my domain model "Value". Are you using the CreateCustomerOrderCommand as a Domain model in this video?
@nickchapsas
@nickchapsas 4 жыл бұрын
That’s correct
@pedroferreira9234
@pedroferreira9234 4 жыл бұрын
@@nickchapsas appreciated, thank you
@clearlyunwell
@clearlyunwell 3 жыл бұрын
👍🏽
@AndrewRoberts42
@AndrewRoberts42 4 жыл бұрын
Hi Nick, How would you do FluentValidation for a PUT request where you may have a [FromRoute] int CustomerId, [FromRoute] int ProductId and [FromBody] UpdateCustomerOrderCommand in the Controller.
@nickchapsas
@nickchapsas 4 жыл бұрын
I would wrapp all 3 of those things in a UpdateCustomerOrderRequest, which contains all those properties with their attributes and create an AbstractValidator. Then you can simply validate on that.
@AndrewRoberts42
@AndrewRoberts42 4 жыл бұрын
@@nickchapsas Does it look like this? End up with multiple classes (the Command and the wrapped class) inheriting from IRequest Bit confused about the use of a Command object in this scenario where the Command represents the body but the domain logic also needs the Route/Query params Should the UpdateCustomerOrderRequest become the Command object? (is this the best place to ask code questions?) public class UpdateCustomerOrderRequest: IRequest //should this be the Command object? { [FromRoute] internal Guid OrderId { get; set; } [FromBody] internal UpdateCustomerOrderCommand Command { get; set; } //should this be a POCO? } public class UpdateCustomerOrderCommand : IRequest { public Guid CustomerId { get; set; } public Guid ProductId { get; set; } } [HttpPut("{orderId}")] public async Task UpdateOrder(UpdateCustomerOrderRequest request) { await _mediator.Send(request); return Ok(); }
@asitkumarmohanty2579
@asitkumarmohanty2579 4 жыл бұрын
I couldn't able to find the source code on your website. Can you please provide. Thanks in advance.
@dfytq
@dfytq 3 жыл бұрын
By default mediatr.send method runs in synchronous way. How to configure it so that it works on asynchronous way?
@nickchapsas
@nickchapsas 3 жыл бұрын
It doesn't. The synchronous way has actually been completely removed for a long time now.
@dfytq
@dfytq 3 жыл бұрын
@@nickchapsas I didn't know that. Thanks. Is it possible to setup multiple handler for a single command? And is it possible to setup multiple handler for single iNotification? Will both handler be executed same time?
@nickchapsas
@nickchapsas 3 жыл бұрын
@@dfytq Yes you can. Notifications is a one to many relationships and all notification handlers will run when a notification is sent
@johanwiersma2242
@johanwiersma2242 4 жыл бұрын
Trying your sample using asp.net core 3.1, but i can't find the services.AddValidatorsFromAssembly() function?
@ganirban82
@ganirban82 4 жыл бұрын
chg it to dotnetcore 3.0
@AscottDev
@AscottDev 4 жыл бұрын
I use services.AddValidatorsFromAssembly(Assembly.Load("full.assembly.name"));
@Eamo-21
@Eamo-21 3 жыл бұрын
Hi Nick, do you have any video on testing the fluent validation? Would you just use some integration tests?
@nickchapsas
@nickchapsas 3 жыл бұрын
I would use unit tests and integration tests
@darkogele
@darkogele 3 жыл бұрын
Do you have the code from this somewhere Nick? And can i have the link if u got it :)
@nickchapsas
@nickchapsas 3 жыл бұрын
The code for all my videos is available to my Patreons
@F2H16
@F2H16 3 жыл бұрын
Where can I get the source code ? Could you please help me to find the source code?
@nickchapsas
@nickchapsas 3 жыл бұрын
The source code is available by becoming a Patreon member
@rainron2664
@rainron2664 2 жыл бұрын
Sir want to clear up some confusion, what is the diff. between IPostHandler and IPipelineBehavior? thank you.😊
@mmsky6316
@mmsky6316 4 жыл бұрын
Great video but I can't access the source code either on github (I also subscribed to the mailing list)
@nickchapsas
@nickchapsas 4 жыл бұрын
Please send me your GitHub username and I will manually invite you
@mmsky
@mmsky 4 жыл бұрын
@@nickchapsas I spoke too soon. I was able to access it! Thank you!
@v.kozenko
@v.kozenko 4 жыл бұрын
hi, what do u think about using ActionFilter instead MediatR?
@v.kozenko
@v.kozenko 4 жыл бұрын
public class ValidationFilter : IAsyncActionFilter { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!context.ModelState.IsValid) { var modelstateErrors = context.ModelState.Where(x => x.Value.Errors.Count > 0) .ToDictionary(k => k.Key, v => v.Value.Errors.Select(x => x.ErrorMessage).ToArray()); var validationResponse = new ValidationResponse(); validationResponse.GlobalValidationMessage = "Invalid form value"; foreach (var error in modelstateErrors) { foreach (var message in error.Value) { validationResponse.ValidationErrors.Add(new ValidationErrorModel { FieldName = error.Key, Message = message }); } } context.Result = new BadRequestObjectResult(validationResponse); return; } await next(); } }
@dariogriffo
@dariogriffo 3 жыл бұрын
No need to use a behaviour. A Preprocessor is more suitable for this.
@codematrix
@codematrix 3 жыл бұрын
Too much abstraction. This will be difficult to debug as you can’t drill down from the mediatR to the final handler and need to manually search for the handlers. Sounds cool and is decoupled, but the over use of design patterns everywhere increases architectural complexity. In other words don’t over engineer because you found a new tool. Always have a good balance. You don’t need this for CQRS. Use straight DI. Also meditatR makes it difficult to inject fakes during unit testing.
@nickchapsas
@nickchapsas 3 жыл бұрын
MediatR makes it very easy to inject fakes during unit testing.
Clean service decoration in .NET Core using Scrutor
11:22
Nick Chapsas
Рет қаралды 36 М.
Clean ASP.NET Core API using MediatR and CQRS | Setup
22:39
Nick Chapsas
Рет қаралды 321 М.
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 14 МЛН
If Barbie came to life! 💝
00:37
Meow-some! Reacts
Рет қаралды 66 МЛН
I'm Excited To see If Kelly Can Meet This Challenge!
00:16
Mini Katana
Рет қаралды 36 МЛН
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 59 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 256 М.
Model Validation | Validation Attributes vs. FluentValidation | .NET 6
27:11
You are doing .NET logging wrong. Let's fix it
25:29
Nick Chapsas
Рет қаралды 171 М.
How to Implement API Versioning for Minimal APIs | ASP.NET Core 8
18:11
Milan Jovanović
Рет қаралды 14 М.
Intro to MediatR - Implementing CQRS and Mediator Patterns
1:21:50
IAmTimCorey
Рет қаралды 222 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 143 М.
Clean Architecture with ASP.NET Core 8 | .NET Conf 2023
29:17
You DON'T want an In-Memory Event Bus like MediatR
11:20
CodeOpinion
Рет қаралды 22 М.
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 14 МЛН