If you want to see more content around MediatR, check out the below 👍 ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ Previous Video 1: ruclips.net/video/mdzEKGlH0_Q/видео.html Previous Video 2: ruclips.net/video/ET00fzkPLVU/видео.html Previous Video 3: ruclips.net/video/86x_HEGOzws/видео.html
I agree with you in the exception thing that they should not be used for control-flow. Using exceptions for control-flow is slower and it has other issues too. After all this a better implementation for validation. I was searching for something like this.
How do you handle the Response record when what is being returned is a custom type, not a basic type like int or string? Like if I have a PersonModel class type that has several properties and the handler is handling data that returns a PersonModel?
Lots of tips and tricks in 15 minutes. I will have to take some time and un-pack your approach, I have been throwing ValidationException and is costly. Thanks a lot mate.
Hi; in one of his talks, jimmy bogard said he only uses validation behavior for "local" validation (can't remember the exact term he used) - what he meant was that validation would only validate the request data itself, but not against the database. Is there anything I am overlooking on why he recommends this? Because like you, I also use validation behavior to check if a certain entity exists at all, or if a certain user with certain permissions is allowed to access the entity, or on creating, if there is already an entity with such a name.
Sorry I haven’t seen that talk. Would love to see it if you have a link to it so I can try and understand his reasoning. But I personally don’t see an issue to before domain level validation inside of the behaviours
@@jonowilliams26 I got you: ruclips.net/video/SUiWfhAhgQw/видео.html (timestamp should direct you to the 45:13 mark, where he starts talking about validation and introduces his two different concepts of validation. His remark I was referring to in my comment, is around 46:30; He doesn't seem to give reasoning, other than he "doesn't want to complicate things".
@@insa07 okay after watching the part where he talks about validation. So it sort of aligns with what I said, where the request validation / api validation should be handled separately. I think he has just chosen to do the validation inside of the command handler. Which I think is also a good approach 👍
@@jonowilliams26 that's great news! Oh, one other question I had was ... what are your thoughts around calling commands from within commands? There might be a scenario where you need to update an entity but also need to update a different entity at the same time? At the moment I'm just using Mediatr to publish an event, and that event triggers a command to update the other entity .. my attempt to keep commands simple. Not sure if there's a better way.
@@DarrynFloodBeachLife Using the same answer to every engineering question. It depends haha. My preference is to avoid it where possible because you want the each command or query to be able to evolve independently. When you are calling one from another then they are coupled. But they may be what actually needs to happen, so rather than abstracting the coupling using Mediatr, just update the other entity inside the command
Hey, @Jonathan great tutorial. Instead of having 2 constructors in the validation pipeline what I did was to check when the request was implementing the interface.
started updating a solution at work which is already built with mediator/cqrs but uses exceptions for management of process flow. We are Using Fluent Validations , and unfortunately CQRSResponse class wouldn't work for us but the nuget package FluentResults is similar to CQRSResponse except it allows the stacking of multiple errors. Thank you very much for this video.
@@davidmata3104 in the controller endpoint call, when I return the fluent result I do return await Return(result); and the result object can be return Result.Ok() or return Result.Fail("error message"); you can also for fail you can stack the errors.
Hi, great videos! I have a question. Say that I in the validation make a call to some API to get some data for the validation. But, I also need that data in the handler. I don´t want to make that call again. Any thoughts on how to solve that?
Great question ! I think the solution would be adding a scoped service which had a dictionary of objects. Inject this service into the validation handler and set the object in the dictionary. Also inject the service into the request handler and get the object from the dictionary using the same key
Interesting, I will look into that next time. I have also thought about using caching (perhaps with PostSharp), so the handler will make the API call, but get cached data. Do you see any disadvantages with that?
since web api automatically managing validation via attributes we supply, i do not simply see how the added complexity benefits us. can we add/edit validation rules on runtime by reading from a config file maybe? if maybe rules changing day to day. i think i have not met the needed complex requirements to see how this validation structure adds value. all in all, thanks for your help, and effort you put in. much appreciated
In the video I mention API validation and domain validation. This will only be used for domain validation. I agree, we should be using built in .NET features to confirm the request is malformed / fields are supplied etc. But the api layer shouldn’t need to know about the domain logic. Hope this helps. Thanks for the feedback 👍
Hi@@jonowilliams26 . For example, creating a new user in the database. What can be considered API validations and what can be considered domain validations? 1.- User has to be an email address 2.- Password should contain 8 to 9 characters and an upper case and a special character. 3.- The user should not exist already in the database. The first one can be easily be done by adding the email attribute but the others are more complex and the 3 of them can be added to the validator. I am still confused about what to add as attribute validations in the model and what to add in the validator. The only advantage that I see to left some validations in the attribute is when using MVC and you want to return a view with the errors in the ModelState.
Brilliant and Thanks. How to return a specific status code and response data from the handler. For eg the handler needs to calls an external API if the external API fails it needs to send the same status code and response data back. If the external API call is successful then the handler will return a List of DTO objects.
You save my life with those tips and tricks! I have juste one question, I have to return a Response whose some of its properties are hidden or shown based on User (ClaimsPrincipal) type (role). Can I use this as validation? Sorry I'm new in .net Web API and I have to deal with a new project.
I think what you are after is using inheritance. Create a base response with common properties across all. Then create sub classes with the specific properties
Validation isnt an exception behaviour it’s expected. Also, exceptions are expensive, in the past i was working on a system where they would throw lots of exceptions for validation handling and it really slowed the system down
@@jonowilliams26 turns out the asp.net core performance best practice states to Minimize Exceptions and that they should be rarely used. Microsoft Article titled, "ASP.NET Core Performance Best Practices". So your approach is on point. great work.
Thanks a bunch. I think we could do the same with exceptions. We could make a base exception class then create custom ones from it. After that, in the filter, we could catch the base exception and return the error message/code accordingly.
Yeah we could definitely do the same with exceptions. The only reason why I avoid throw exceptions for this kind of stuff is because it’s very expensive and uses much more resources. I had an example in the past were the app was throwing lots of exceptions for stuff that was expected to happen really slowed the system down
You getting wrong whether NotNull and NotBlank validations are either domain or application specific. Your domain shouldn't allow todos to have empty names simply because you can't describe a task with no words to it.
If you want to see more content around MediatR, check out the below 👍
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Previous Video 1: ruclips.net/video/mdzEKGlH0_Q/видео.html
Previous Video 2: ruclips.net/video/ET00fzkPLVU/видео.html
Previous Video 3: ruclips.net/video/86x_HEGOzws/видео.html
Absolute blast.
First video: Liked, subscribed.
Honestly man I am really impressed by your videos man! Your quality and explanation is insanely good, keep it up, it is seriously awesome.
Thank you so much !
i really like your video .finish the last episode . good job.
I love your approach. Hoping for more videos!
Thanks mate ! I have been abit slack lately on creating new videos. So hopefully some more coming soon !
@@jonowilliams26 Yes please. Your vids are a big help 💯👍
Congratulations Jonathan, you teach very simply
I was waiting for this!
Amazing tips! I'm waiting for your last video in this series
Thank you so much for the positive feedback ! Unfortunately, RUclips has taken a back seat at the moment. Been really busy at work lately
I agree with you in the exception thing that they should not be used for control-flow. Using exceptions for control-flow is slower and it has other issues too. After all this a better implementation for validation. I was searching for something like this.
Thanks ! Glad you liked it 👍
@@jonowilliams26 yes. it was awesome. although I'm very new to the mediator, you made it a piece of cake for me.
How do you handle the Response record when what is being returned is a custom type, not a basic type like int or string? Like if I have a PersonModel class type that has several properties and the handler is handling data that returns a PersonModel?
Lots of tips and tricks in 15 minutes. I will have to take some time and un-pack your approach, I have been throwing ValidationException and is costly. Thanks a lot mate.
All good Junior ! Glad the video was able to help 👍
Hi; in one of his talks, jimmy bogard said he only uses validation behavior for "local" validation (can't remember the exact term he used) - what he meant was that validation would only validate the request data itself, but not against the database. Is there anything I am overlooking on why he recommends this? Because like you, I also use validation behavior to check if a certain entity exists at all, or if a certain user with certain permissions is allowed to access the entity, or on creating, if there is already an entity with such a name.
Sorry I haven’t seen that talk. Would love to see it if you have a link to it so I can try and understand his reasoning. But I personally don’t see an issue to before domain level validation inside of the behaviours
@@jonowilliams26 I got you: ruclips.net/video/SUiWfhAhgQw/видео.html (timestamp should direct you to the 45:13 mark, where he starts talking about validation and introduces his two different concepts of validation. His remark I was referring to in my comment, is around 46:30; He doesn't seem to give reasoning, other than he "doesn't want to complicate things".
@@insa07 thanks for that. I’ll have a watch and see what he says 👍
@@insa07 okay after watching the part where he talks about validation. So it sort of aligns with what I said, where the request validation / api validation should be handled separately. I think he has just chosen to do the validation inside of the command handler. Which I think is also a good approach 👍
These videos are so helpful ... thanks mate! Are you planning on doing a video on unit testing Mediatr Commands/Queries/etc?
Thanks the feedback! I’m glad you liked the video! I’m adding it to my backlog. Had multiple requests for it so will probably get to it soon !
@@jonowilliams26 that's great news! Oh, one other question I had was ... what are your thoughts around calling commands from within commands? There might be a scenario where you need to update an entity but also need to update a different entity at the same time? At the moment I'm just using Mediatr to publish an event, and that event triggers a command to update the other entity .. my attempt to keep commands simple. Not sure if there's a better way.
@@DarrynFloodBeachLife Using the same answer to every engineering question. It depends haha. My preference is to avoid it where possible because you want the each command or query to be able to evolve independently. When you are calling one from another then they are coupled. But they may be what actually needs to happen, so rather than abstracting the coupling using Mediatr, just update the other entity inside the command
Hey, @Jonathan great tutorial.
Instead of having 2 constructors in the validation pipeline what I did was to check when the request was implementing the interface.
started updating a solution at work which is already built with mediator/cqrs but uses exceptions for management of process flow. We are Using Fluent Validations , and unfortunately CQRSResponse class wouldn't work for us but the nuget package FluentResults is similar to CQRSResponse except it allows the stacking of multiple errors. Thank you very much for this video.
Hey, Sam can you explain more how you did it?
@@davidmata3104 in the controller endpoint call, when I return the fluent result I do return await Return(result); and the result object can be return Result.Ok() or return Result.Fail("error message"); you can also for fail you can stack the errors.
@@SolomonTheStoryTeller That sounds great, instead of throwing an exception. Do you have a git repository where I can see an example?
@@davidmata3104 the implementation there can be improved and simplified but it should help you understand how to use fluent results
@@davidmata3104 RUclips keeps removing my comments. Plexripper repo
i am searching this only thanks
Hi, great videos!
I have a question. Say that I in the validation make a call to some API to get some data for the validation. But, I also need that data in the handler. I don´t want to make that call again. Any thoughts on how to solve that?
Great question ! I think the solution would be adding a scoped service which had a dictionary of objects. Inject this service into the validation handler and set the object in the dictionary. Also inject the service into the request handler and get the object from the dictionary using the same key
Interesting, I will look into that next time. I have also thought about using caching (perhaps with PostSharp), so the handler will make the API call, but get cached data. Do you see any disadvantages with that?
@@mikaeljirhage7679 yeah that sounds like a solid plan as well !
since web api automatically managing validation via attributes we supply, i do not simply see how the added complexity benefits us. can we add/edit validation rules on runtime by reading from a config file maybe? if maybe rules changing day to day. i think i have not met the needed complex requirements to see how this validation structure adds value. all in all, thanks for your help, and effort you put in. much appreciated
In the video I mention API validation and domain validation. This will only be used for domain validation. I agree, we should be using built in .NET features to confirm the request is malformed / fields are supplied etc. But the api layer shouldn’t need to know about the domain logic. Hope this helps. Thanks for the feedback 👍
Hi@@jonowilliams26 . For example, creating a new user in the database. What can be considered API validations and what can be considered domain validations?
1.- User has to be an email address
2.- Password should contain 8 to 9 characters and an upper case and a special character.
3.- The user should not exist already in the database.
The first one can be easily be done by adding the email attribute but the others are more complex and the 3 of them can be added to the validator.
I am still confused about what to add as attribute validations in the model and what to add in the validator.
The only advantage that I see to left some validations in the attribute is when using MVC and you want to return a view with the errors in the ModelState.
Sorry to be that guy but whats your vscode theme and font?
Brilliant and Thanks. How to return a specific status code and response data from the handler. For eg the handler needs to calls an external API if the external API fails it needs to send the same status code and response data back. If the external API call is successful then the handler will return a List of DTO objects.
Hi Thank you so much for your videos ! Do you plan to make a video with MediatR and Fluent Validation ?
Thanks ! Glad you like them. Nah I don’t plan too. Haven’t used fluent validation so I might check it out
You save my life with those tips and tricks! I have juste one question, I have to return a Response whose some of its properties are hidden or shown based on User (ClaimsPrincipal) type (role). Can I use this as validation? Sorry I'm new in .net Web API and I have to deal with a new project.
I think what you are after is using inheritance. Create a base response with common properties across all. Then create sub classes with the specific properties
@@jonowilliams26 Good idea, thank you!
What is wrong with throwing exceptions?
Validation isnt an exception behaviour it’s expected. Also, exceptions are expensive, in the past i was working on a system where they would throw lots of exceptions for validation handling and it really slowed the system down
@@jonowilliams26 turns out the asp.net core performance best practice states to Minimize Exceptions and that they should be rarely used. Microsoft Article titled, "ASP.NET Core Performance Best Practices". So your approach is on point. great work.
Hi where is the link to your GitHub
In the description 👍
Thanks a bunch. I think we could do the same with exceptions. We could make a base exception class then create custom ones from it. After that, in the filter, we could catch the base exception and return the error message/code accordingly.
Yeah we could definitely do the same with exceptions. The only reason why I avoid throw exceptions for this kind of stuff is because it’s very expensive and uses much more resources. I had an example in the past were the app was throwing lots of exceptions for stuff that was expected to happen really slowed the system down
You getting wrong whether NotNull and NotBlank validations are either domain or application specific. Your domain shouldn't allow todos to have empty names simply because you can't describe a task with no words to it.
Disappointed!
I came here to watch that beautiful theme in the thumbnail but disappointed to see ugly default theme of visual studio.