Coding Shorts: Minimal API Endpoint Filters for Model Validation

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ส.ค. 2024

ความคิดเห็น • 27

  • @michaelduren3907
    @michaelduren3907 28 วันที่ผ่านมา

    Such a great simple video. Appreciate it thank you

  • @Octopie18
    @Octopie18 ปีที่แล้ว +2

    Very informative video! I am starting to sway towards minimal APIs now, thanks to your videos. Keep it up Shawn!

  • @Praveen.Kumar.
    @Praveen.Kumar. ปีที่แล้ว +1

    Great effort. Thank you for sharing.

  • @bayantsinghkang67
    @bayantsinghkang67 ปีที่แล้ว +1

    Thanks Shawn!

  • @spontaneousorder5670
    @spontaneousorder5670 ปีที่แล้ว +1

    I really like your presentation style. Nice video.

  • @bric305
    @bric305 ปีที่แล้ว

    Just discovered your channel, I already love it!

  • @IlyaArlenka
    @IlyaArlenka 6 หลายเดือนก่อน

    Great explanation, thanks a lot!

    • @swildermuth
      @swildermuth  6 หลายเดือนก่อน

      You are welcome!

  • @hangor3620
    @hangor3620 6 หลายเดือนก่อน

    Was helpful, thanks!

  • @alphaios7763
    @alphaios7763 ปีที่แล้ว +1

    I just got stuck with this in my tinkerimg with minimal APIs, I was trying to do this with middleware, but it is impossible to extract those generic parameters through HttpContext. Thank you very much for showing this!

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      Great, though this technique only works for .NET 7.

  • @jamesdoescode
    @jamesdoescode ปีที่แล้ว

    Thanks Shawn, I really like the idea of this style of validation over the attribute-based approach.
    One question though, at 12:26, what's the benefit of .OfType().FirstOrDefault(a => a?.GetType() == typeof(T)); ?
    Couldn't you simply do .OfType().FirstOrDefault(); ?
    It seems like the condition inside FirstOrDefault is just repeating the work of OfType

  • @coding-in
    @coding-in 3 หลายเดือนก่อน

    Hi Shawn, thanks for the great video. i just wanna ask you how to make it generic register AddValidatorsFromAssemblyContaining(typeof(ClassValidator)) ?

    • @swildermuth
      @swildermuth  3 หลายเดือนก่อน

      You can use AddValidatorsFromAssembly or one of the other methods that will look at it more globally. But I don't suggest looking at all of the types to find the validators.

    • @coding-in
      @coding-in 3 หลายเดือนก่อน

      @@swildermuth i finally using SharpGrip.. thanks for the answer

  • @modernkennnern
    @modernkennnern ปีที่แล้ว

    Working with C# in VS Code looks extremely inconvenient; What makes you use it over Rider?

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      For these small examples, VSCode is quicker to get my ideas in the video. I use Visual Studio much more often for general .NET projects. Rider is a great product. I don't see the benefit since i'm already on Windows. I also don't care for the constant nagging for every suggestion that Resharper thinks I need. But if it helps you, that's awesome. If you're curious my longer take, see my video about whether VSCode is ready for full-time .NET Dev:
      th-cam.com/video/71Hx2pfgym8/w-d-xo.html

  • @TimCromarty
    @TimCromarty ปีที่แล้ว

    I am using a very similar pattern, but finding re-use difficult when I don't know which of the context Arguments I want to validate. My request has multiple Guids and sometimes my Object Id is the first parameter, other times it may be the second or third.
    You are using .FirstOrDefault(a => a?.GetType() == typeof(T)...
    How could you cater for cases where it is not the first parameter?

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      I know this looks like it has to be the first parameter, but it doesn't. Remember, you're validating a specific Type (T), so this is finding the first (or default) that has that type. If you're validating more than one parameter of the same type, you could get a collection from this, but I can't imagine that is actually what you're doing. If you look at the source:
      github.com/shawnwildermuth/codingshorts/blob/742e661714885bf8652760f8e7c1c625b1ec66af/minimal-validation/after/ValidationFilter.cs#L11

    • @TimCromarty
      @TimCromarty ปีที่แล้ว

      @@swildermuth i do have multiple parameters of the same type, in my case it’s 3 Guids coming from route parameters.
      however, I have now found a solution. By building the filter from the EndpointFilterFactory I get access to the EndpointFilterFactoryContext which gives me the parameters collection complete with names, types snd positions. I can therefore look up the position based on the parameter name and I can re-use the filter irrespective of where in the parameter list my input value is coming from. Thanks

  • @pilotboba
    @pilotboba ปีที่แล้ว

    Is doing validation with Mediatr pipelines out of vouge with Minimal API now?

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      When was it in vogue. I haven't done it before. Can you explain?

    • @pilotboba
      @pilotboba ปีที่แล้ว

      @@swildermuth In very basic terms.
      You would create your abstract validator as you've already done.
      Next, you will create a ValidationBehaviour which implements IPipelineBehavior provided by mediatr.
      Next you will register your pipeline behaviors in DI.
      Now, when a request comes in an you send it (command or query) the validator for that request will be run before the handler is called. Other behaviors may also run, and caching or logging or auth whatever.
      Very common is to have the validationbahavior throw a validation exception and then have a middleware that sees that and coverts it to a problem response.
      I prefer to return a Result type. But what ever works.
      I'm sure if you search for mediatr pipeline validation behavior you can find more detailed info.