MediatR Validation Behavior - Validation In C# Web API

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025
  • НаукаНаука

Комментарии • 59

  • @jonowilliams26
    @jonowilliams26  3 года назад +9

    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

  • @quentincouissinier8533
    @quentincouissinier8533 3 года назад

    Absolute blast.
    First video: Liked, subscribed.

  • @hendrik2765
    @hendrik2765 3 года назад +3

    Honestly man I am really impressed by your videos man! Your quality and explanation is insanely good, keep it up, it is seriously awesome.

  • @abolfazlafshar4241
    @abolfazlafshar4241 3 года назад

    i really like your video .finish the last episode . good job.

  • @qwerty5689
    @qwerty5689 3 года назад +1

    I love your approach. Hoping for more videos!

    • @jonowilliams26
      @jonowilliams26  3 года назад

      Thanks mate ! I have been abit slack lately on creating new videos. So hopefully some more coming soon !

    • @qwerty5689
      @qwerty5689 3 года назад +1

      @@jonowilliams26 Yes please. Your vids are a big help 💯👍

  • @bsouzafilipe
    @bsouzafilipe 3 года назад

    Congratulations Jonathan, you teach very simply

  • @victor1882
    @victor1882 3 года назад +1

    I was waiting for this!

  • @angelikakuleta
    @angelikakuleta 3 года назад +1

    Amazing tips! I'm waiting for your last video in this series

    • @jonowilliams26
      @jonowilliams26  3 года назад

      Thank you so much for the positive feedback ! Unfortunately, RUclips has taken a back seat at the moment. Been really busy at work lately

  • @zhh174
    @zhh174 3 года назад +1

    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.

    • @jonowilliams26
      @jonowilliams26  3 года назад

      Thanks ! Glad you liked it 👍

    • @zhh174
      @zhh174 3 года назад

      @@jonowilliams26 yes. it was awesome. although I'm very new to the mediator, you made it a piece of cake for me.

  • @j_ason_rpg
    @j_ason_rpg 3 года назад

    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?

  • @junianohjj4602
    @junianohjj4602 3 года назад +1

    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.

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      All good Junior ! Glad the video was able to help 👍

  • @insa07
    @insa07 3 года назад +2

    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.

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      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

    • @insa07
      @insa07 3 года назад +1

      @@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".

    • @jonowilliams26
      @jonowilliams26  3 года назад

      @@insa07 thanks for that. I’ll have a watch and see what he says 👍

    • @jonowilliams26
      @jonowilliams26  3 года назад

      @@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 👍

  • @DarrynFloodBeachLife
    @DarrynFloodBeachLife 3 года назад +1

    These videos are so helpful ... thanks mate! Are you planning on doing a video on unit testing Mediatr Commands/Queries/etc?

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      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 !

    • @DarrynFloodBeachLife
      @DarrynFloodBeachLife 3 года назад

      @@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.

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      @@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

  • @davidmata3104
    @davidmata3104 3 года назад

    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.

  • @SolomonTheStoryTeller
    @SolomonTheStoryTeller 3 года назад +3

    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
      @davidmata3104 3 года назад

      Hey, Sam can you explain more how you did it?

    • @SolomonTheStoryTeller
      @SolomonTheStoryTeller 3 года назад +1

      @@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.

    • @davidmata3104
      @davidmata3104 3 года назад

      @@SolomonTheStoryTeller That sounds great, instead of throwing an exception. Do you have a git repository where I can see an example?

    • @SolomonTheStoryTeller
      @SolomonTheStoryTeller 3 года назад

      @@davidmata3104 the implementation there can be improved and simplified but it should help you understand how to use fluent results

    • @SolomonTheStoryTeller
      @SolomonTheStoryTeller 3 года назад +1

      @@davidmata3104 RUclips keeps removing my comments. Plexripper repo

  • @priyanshu4016
    @priyanshu4016 3 года назад

    i am searching this only thanks

  • @mikaeljirhage7679
    @mikaeljirhage7679 3 года назад +1

    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?

    • @jonowilliams26
      @jonowilliams26  3 года назад

      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

    • @mikaeljirhage7679
      @mikaeljirhage7679 3 года назад +1

      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?

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      @@mikaeljirhage7679 yeah that sounds like a solid plan as well !

  • @tuberklz
    @tuberklz 3 года назад +1

    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

    • @jonowilliams26
      @jonowilliams26  3 года назад +3

      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 👍

    • @davidmata3104
      @davidmata3104 3 года назад

      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.

  • @bayobizzle
    @bayobizzle 3 года назад

    Sorry to be that guy but whats your vscode theme and font?

  • @yogeswarank
    @yogeswarank 3 года назад

    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.

  • @Camille-jm2oe
    @Camille-jm2oe 3 года назад +1

    Hi Thank you so much for your videos ! Do you plan to make a video with MediatR and Fluent Validation ?

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      Thanks ! Glad you like them. Nah I don’t plan too. Haven’t used fluent validation so I might check it out

  • @zakariabenmassaoud
    @zakariabenmassaoud 3 года назад

    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.

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      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

    • @zakariabenmassaoud
      @zakariabenmassaoud 3 года назад

      @@jonowilliams26 Good idea, thank you!

  • @SolomonTheStoryTeller
    @SolomonTheStoryTeller 3 года назад

    What is wrong with throwing exceptions?

    • @jonowilliams26
      @jonowilliams26  3 года назад

      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

    • @SolomonTheStoryTeller
      @SolomonTheStoryTeller 3 года назад +1

      @@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.

  • @muatassim8
    @muatassim8 3 года назад

    Hi where is the link to your GitHub

  • @zakiit9594
    @zakiit9594 3 года назад +1

    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.

    • @jonowilliams26
      @jonowilliams26  3 года назад +2

      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

  • @mateussarmento7692
    @mateussarmento7692 2 года назад

    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.

  • @apoorvrane3951
    @apoorvrane3951 3 года назад

    Disappointed!
    I came here to watch that beautiful theme in the thumbnail but disappointed to see ugly default theme of visual studio.