Dependency Injection in Minimal APIs in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ค. 2023
  • Minimal APIs are an increasingly common way to create small API projects including microservices. But how do you handle dependency injection with a Minimal API? It turns out the answer to that is rather simple. In this video, I will show you how to use dependency inject, route parameters, and more in 10 minutes or less.
    Full Training Courses: IAmTimCorey.com
    Source Code: leadmagnets.app/?Resource=Min...
    Mailing List: signup.iamtimcorey.com/

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

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

    Excelente video, esa forma de enseñar tuya es muy fácil de entender. Llevo dos meses siguiéndote y vale mucho la pena tu información.

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

      I am glad it has been helpful.

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

    So clear and simple.
    Tnx, Tim

  • @albansejdiu
    @albansejdiu ปีที่แล้ว +7

    Thanks for the great video Tim!
    It would be great if you could also reference your previous video links for things which are seen in this video, e.g. using Http files in VS, so that people which see those things for the first time can catch-up quickly.

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว +3

      Thanks for the suggestion.

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

    Thank you for the short and great video :)

  • @parkerwarner8688
    @parkerwarner8688 ปีที่แล้ว +3

    Hey Tim I really like your content and have learned a lot. I think you should consider giving a more “production” or “large-scale” scenario with videos. I often find the basics are usually straightforward but then seeing how it could be used in a large scale app could be useful as well

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

      There's a balance I try to hit. The key is how to address the audience as a whole. I try to cover topics at the same percentage rate as my audience. For instance, not many people in the audience really need heavy scalability, so I try to cover that sparingly. A lot of the audience needs to learn new technologies or techniques, so I cover them more. However, I'm always looking for new suggestions. If you have something specific you want to see covered, leave a suggestion at suggestions.iamtimcorey.com

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

      @@IAmTimCorey totally understandable. I appreciate the response

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

    As always clear and precise! The simple examples are the root for better understanding, thanks!

  • @torrvic1156
    @torrvic1156 7 หลายเดือนก่อน

    Looks very unusual. Interesting…Thanks for sharing!

    • @IAmTimCorey
      @IAmTimCorey  7 หลายเดือนก่อน +1

      You are welcome.

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

    Great video ! Thanks for sharing!

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

    Thanks for the great video. In MVC there's the [FromServices] attribute that we apply to function parameters for this sort of thing. I take it that idea didn't map over to Minimal APIs too well.

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

      I think it was more that they were trying to eliminate the need to add an attribute if it could be inferred where it came from.

  • @user-hu7me6sk2n
    @user-hu7me6sk2n 9 หลายเดือนก่อน +1

    A little off topic, but when was the 'with' keyword implemented in c#? First time I have come across it!

  • @marcioalexandremarcondes557
    @marcioalexandremarcondes557 9 หลายเดือนก่อน

    Tks a lot!

    • @IAmTimCorey
      @IAmTimCorey  9 หลายเดือนก่อน

      You are welcome.

  • @Alex-ol4bv
    @Alex-ol4bv ปีที่แล้ว +2

    Great video, short and concise.
    How would I go about registering my own services, e. g. for Data Access? Where would I put that inside of program.cs?

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

      This video (it links to the correct timecode) shows you how to add your own dependencies to Minimal API: th-cam.com/video/5tYSO5mAjXs/w-d-xo.html

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

    Nice explanation

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

    So stuff from the appsettings json is the only thing we can inject in this kind of APIs?

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

      No, you can inject any dependency you want. I just demonstrated the configuration because that was already set up and it is something you would commonly use.

  • @timur2887
    @timur2887 4 หลายเดือนก่อน

    Great! But how this works under the hood? I mean passing values from DI-container to lamda parameters of mapping methods?

    • @IAmTimCorey
      @IAmTimCorey  4 หลายเดือนก่อน +1

      That's part of the .NET system. You can look at the source code for .NET on GitHub if you want, but the basics are that it figures out if you are asking for a dependency item. If so, it provides it. If not, it figures out where it can pull that data from (URL, body, etc.)

    • @timur2887
      @timur2887 4 หลายเดือนก่อน

      @@IAmTimCorey thank you for your reply!

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

    what is an actual real world use case for this?

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

      For dependency injection? It is used in most .NET applications. If you mean Minimal APIs, they are typically used when you just need a small API or a microservice.

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

    Is there any relevance in the real world for minimal api? I am a seasoned software developer and I haven’t seen this being used. I’m not bashing rather trying to understand the usefulness and adoption status of this technology

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

      I'm operating 20 services with minimal API approach.
      They're very handy and easy to setup, maintain and extend.
      And they're faster than controller based APIs.
      This is maybe BS but in my opinion they're easier to test as well. - but this depends highly on the style how you build them.

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว +3

      Absolutely. One thing that is important to note, though, is that technologies are not adopted overnight. Minimal APIs just came out in November 2022. That's less than a year. There are definitely companies using them (see @TuxCommander's comment), but companies don't typically replace existing technologies with new ones. They build new things in the latest tech, when they can. That means that Minimal APIs will slowly invade the space rather than just wholesale replacing controller-based APIs.
      Also, don't forget that Minimal APIs aren't a replacement for controller-based APIs in every scenario. Instead, they are focused on microservices, small APIs, etc.

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

    Could you please make video on below question in DOT NET.I got this in an interview
    Diffrence between Abstract Class and Concreate class
    Difference between Finalize Method and Finalize block
    Diffrent type of Garbage Collection method
    how to handle Exception using Middleware in Dot Net core
    Exposing Entity will create problem in Entity Framework How we handle it
    How we validate Entity in Entity Framework
    Two Interface have same method how we implement and call?
    without using loop how we do bulk insert and update in sql server?
    how map the data from model to database table if the column are diffrent?
    how map data from list to model using linq query?
    how join two table using linq?
    how join two list using linq?
    what happen if action parameter not matched with any method?
    what is scope of a classs object?
    how garbage collector know which objects needs to be deleted?
    Entity framework -model mapping and how many type of model

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

  • @richardhauer4696
    @richardhauer4696 ปีที่แล้ว +4

    None of this is dependency injection? No service registration. No lifetimes. No container setup at all. This is just getting config values, and you didn’t even show object binding which would have been simpler than the “:” notation.

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

      This is dependency injection. We are injecting dependencies into our endpoints. It sounds like you are looking for dependency injection configuration. Here is a link to where I do that in a Minimal API: th-cam.com/video/5tYSO5mAjXs/w-d-xo.html
      The point of this video wasn't to cover how to set up dependency injection. It was to show how to use it in a Minimal API. As for object binding, that's not simpler for simple lookups. It is simpler when you are looking up multiple items repeatedly.

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

      It's bullshit injection 😂

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

    Need to calm that mouse down. Super distracting seeing the cursor draw circles all over the place.

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

      I'll try to give it less caffeine next time.