Dependency Injection in Minimal APIs in C#

Поделиться
HTML-код
  • Опубликовано: 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.

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

  • @UmanPC
    @UmanPC Год назад

    So clear and simple.
    Tnx, Tim

  • @shahzadazzam
    @shahzadazzam Год назад

    Thank you for the short and great video :)

  • @pavfrang
    @pavfrang Год назад +2

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

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

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

  • @ElTioDev
    @ElTioDev Год назад +1

    Great video ! Thanks for sharing!

  • @torrvic1156
    @torrvic1156 7 месяцев назад

    Looks very unusual. Interesting…Thanks for sharing!

  • @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: ruclips.net/video/5tYSO5mAjXs/видео.html

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

  • @vishalmakam7291
    @vishalmakam7291 Год назад

    Nice explanation

  • @marcioalexandremarcondes557
    @marcioalexandremarcondes557 8 месяцев назад

    Tks a lot!

  • @pxnchx93
    @pxnchx93 11 месяцев назад

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

    • @IAmTimCorey
      @IAmTimCorey  11 месяцев назад +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!

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

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

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

  • @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: ruclips.net/video/5tYSO5mAjXs/видео.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.