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
  • Наука та технологія

КОМЕНТАРІ • 116

  • @MilanJovanovicTech
    @MilanJovanovicTech  Рік тому +6

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/cqrs-validation

    • @F2H16
      @F2H16 Рік тому +1

      Hi Milan, is it possible to get the access to the source code?

  • @rakkarajput
    @rakkarajput Рік тому +1

    Concept clear now, fully explained
    Thanks Buddy 🥳🥳🥳keep it up

  • @kirillhorn3186
    @kirillhorn3186 Рік тому +1

    Your video is awesome as always!!!

  • @bookuha
    @bookuha Рік тому +2

    Yeeee thank you Milan!

  • @ibrahimzaky3056
    @ibrahimzaky3056 6 днів тому

    this video help me to solve problem ... thank you man

  • @ayanesuarezromero4222
    @ayanesuarezromero4222 Місяць тому

    Very nice video. It helps me make my validation result much cleaner and simpler. Thank you!

  • @roefski
    @roefski Рік тому +11

    Hi Milan, awesome video as always 🙂. Any chance you'll be doing authorization/authentication with a clean architecture setup?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +8

      It's going to happen for sure!

    • @flobuilds
      @flobuilds Рік тому +1

      @@MilanJovanovicTech will you build on top of identity server and implement a decent user management?

  • @timur2887
    @timur2887 2 місяці тому

    Thank you!

  • @AboutCleanCode
    @AboutCleanCode Рік тому +4

    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  Рік тому

      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.

  • @user-ni7ce4fe4k
    @user-ni7ce4fe4k 27 днів тому

    Thanks

  • @omar-fathy01
    @omar-fathy01 5 місяців тому +1

    Awesome ❤ do you have a vertical slice architecture lesson?

  • @pazzuto
    @pazzuto Рік тому +1

    Great video! One comment about the HandleError method... shouldn't the wildcard return a 505 instead of a 404?

  • @abderrazakhenka3903
    @abderrazakhenka3903 Рік тому +1

    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  Рік тому +1

      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.

  • @davidpccode
    @davidpccode 10 місяців тому +2

    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 👍

  • @sphrtehrani
    @sphrtehrani 10 місяців тому +1

    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  10 місяців тому +1

      Different kind of validation. It's a tradeoff I can live with.

  • @codearabawy
    @codearabawy Рік тому +1

    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  Рік тому +1

      If you create multiple AbstractValidator for the same command, they'll be passed in to the behavior

  • @dirceusjr
    @dirceusjr 11 місяців тому +1

    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.

  • @pwn2own23
    @pwn2own23 Рік тому +1

    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  Рік тому

      I don't think about that, to be honest

    • @pwn2own23
      @pwn2own23 Рік тому +1

      @@MilanJovanovicTech hehe, ok. then you never had team-external consumers 🤪

    • @PelFox
      @PelFox Рік тому +6

      @@pwn2own23 there is a nuget for that called MicroElements.Swashbuckle.FluentValidation which adds all your FluentValidation validators to the swagger schema.

  • @empireofhearts
    @empireofhearts Рік тому +1

    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

  • @fieryscorpion
    @fieryscorpion Рік тому +1

    Can you please make a video on logging using Serilog in your setup?

  • @viktorasmickunas2527
    @viktorasmickunas2527 Рік тому +1

    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.

  • @rakkarajput
    @rakkarajput Рік тому +2

    Please create a video on micro services and micro frontend

  • @Omar.bin.khattab
    @Omar.bin.khattab 8 місяців тому

    thanks for your time but keep it simple as possible

  • @vitaliishmidt3704
    @vitaliishmidt3704 Рік тому +1

    Hi Milan. Thanks a lot for sharing such a nice and helpful information. Can you please share the source code of this project?

  • @thomaswoods1365
    @thomaswoods1365 3 місяці тому

    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  3 місяці тому +1

      I'll see what I can do 😁

    • @thomaswoods1365
      @thomaswoods1365 3 місяці тому

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

  • @r75shell
    @r75shell Рік тому +1

    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.

  • @singernooneheard6967
    @singernooneheard6967 Рік тому

    Do we need the problem detail object if we use the result pattern (eg. languageExtcore)

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      As long as you return a standardized response from the API, I would consider it alright

  • @ravindranaths513
    @ravindranaths513 10 місяців тому

    Hi Milan,
    Why IDomainEventHandler is given in Application layer; instead of Domain layer?

  • @aamirali8114
    @aamirali8114 9 місяців тому +1

    i guess youtube was listening meeting between me and my manager who was discussing yo implement CQRS and Fluent Api

    • @MilanJovanovicTech
      @MilanJovanovicTech  9 місяців тому +1

      There's also this version, which is slightly different: ua-cam.com/video/OhQA4PDdp0Q/v-deo.html

  • @harryh212
    @harryh212 Рік тому +1

    Is it possible to have as pipeline like this on a crud style service?

  • @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  Рік тому +4

      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 Рік тому

      @@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 Рік тому +1

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

  • @user-gn7bd6mq8n
    @user-gn7bd6mq8n 3 місяці тому

    Could you please make video on Validation Pipeline Behaviour With Minimal API?

  • @masoudmotlagh6384
    @masoudmotlagh6384 5 місяців тому +1

    Can we reuse this validator in blazor client? is it clean to inject or use command validators in client?

    • @MilanJovanovicTech
      @MilanJovanovicTech  5 місяців тому +1

      Not sure how it would work in Blazor, but you probably can

    • @masoudmotlagh6384
      @masoudmotlagh6384 5 місяців тому

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

  • @user-gn7bd6mq8n
    @user-gn7bd6mq8n 3 місяці тому

    Fluid Milan ❤
    What is IValidationResult here? Does come from FluentValidation?

  • @user-ow6gy6xj5v
    @user-ow6gy6xj5v Рік тому +1

    Hi, how to return FluentValidation.Results.ValidationResult object instead of your custom validation result?

  • @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  Рік тому

      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 Рік тому +1

      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 Рік тому +3

      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!

  • @user-rp2rt6ws8m
    @user-rp2rt6ws8m Рік тому +1

    Hello, can you show your "Result" and "Error" classes?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      You can see an example here: github.com/m-jovanovic/rally-simulator

  • @syedjunaid7846
    @syedjunaid7846 6 місяців тому

    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?

  • @user-ds9fm1sm4e
    @user-ds9fm1sm4e 4 місяці тому +1

    could you provide the code for result class and result class ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e

    • @user-ds9fm1sm4e
      @user-ds9fm1sm4e 2 місяці тому

      @@MilanJovanovicTech Thanks 🤩

  • @DanKac182
    @DanKac182 Місяць тому

    Does it still make sense to use the binding validation on the request body object that .net validate as default?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Місяць тому

      Yes, but if you're using [ApiController] or minimal APIs those will be applied by default

    • @DanKac182
      @DanKac182 Місяць тому

      @@MilanJovanovicTech so both validations would be applied? aren't they doing kind the same in the end of the day?

  • @giovannis6506
    @giovannis6506 4 місяці тому

    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?

  • @norelayaien9888
    @norelayaien9888 Рік тому +1

    I did not realize that in class Result what is it

  • @Tamer_Ali
    @Tamer_Ali Рік тому

    Hi Milan, thanks for your video
    are you going to talk about CQRS and Minimal API in Clean Architecture?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      CQRS was the previous video. And I'm planning Minimal API soon :)

  • @mustafahaider9115
    @mustafahaider9115 11 днів тому

    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

  • @abhikarne
    @abhikarne Рік тому +1

    hey man, you do not show Error Class :(

  • @awmy3109
    @awmy3109 Рік тому +5

    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  Рік тому +3

      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.

  • @pablofonseca8867
    @pablofonseca8867 3 місяці тому

    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  3 місяці тому

      There was basically zero "DDD patterns" in this video. What are you talking about?

  • @muhamotto2084
    @muhamotto2084 7 місяців тому +3

    man you are complicating things too much!! you are using the fluentValidation library, so just use it instead of extending it !

    • @MilanJovanovicTech
      @MilanJovanovicTech  7 місяців тому

      What are you talking about? With the pipeline in place, I can just create validators and they'll be applied automatically

  • @user-gn7bd6mq8n
    @user-gn7bd6mq8n 3 місяці тому

    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));

    • @user-gn7bd6mq8n
      @user-gn7bd6mq8n 3 місяці тому

      I am using .NET 8

    • @user-gn7bd6mq8n
      @user-gn7bd6mq8n 3 місяці тому

      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  3 місяці тому

      Glad you managed to solve it