Dependency Injection Explained Like You’re 5 (with FastAPI Examples)

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

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

  • @codingwithroby
    @codingwithroby  3 месяца назад +2

    There it is folks, dependency injection. What’s your biggest challenge as a developer right now?

  • @calebmatthias1765
    @calebmatthias1765 Месяц назад

    This is so far the best video I have seen on this subject! Thank you for the post

  • @danialmunsif
    @danialmunsif 3 месяца назад +3

    well explained sir

  • @Wolfci
    @Wolfci 3 месяца назад

    More of these please lol just subbed great explanation

  • @BritScientist
    @BritScientist 2 месяца назад +1

    Thank You. Why don't you organize you code in `__init__()` in your classes without creating unnecessary functions next to classes? Dependency injection in FastAPI requires callable, so it may be a class, not only function.

    • @codingwithroby
      @codingwithroby  2 месяца назад +1

      I have learned it is easy for beginners to learn by breaking things out like this. But perhaps in the future I should start implementing in the constructor or class like you are suggesting.

  • @krishnaagarwal7934
    @krishnaagarwal7934 3 месяца назад

    I love love love ur videoss❤❤❤

  • @farazahmed1668
    @farazahmed1668 3 месяца назад +1

    which theme are you using in this tutorial?

    • @codingwithroby
      @codingwithroby  3 месяца назад

      On my VSCode?

    • @farazahmed1668
      @farazahmed1668 3 месяца назад

      @@codingwithroby yes the vs-code theme in this tutorial.
      And also i am very confused while learning fastapi there are so many ways in fastapi even doing a single thing like SQL alchemy vs sqlmodel and then using sa_column in sqlmodel to write SQL alchemy in sqlmodel and then there are so many drivers like psycopg3 vs psycopg2 vs asyncpg, and war does not stop there there are multiple sessions classes and multiple ways to use session like you can use it with context manager or with a context decorator. Every time I try to use async in python I get stuck. I thought django is complicated but in programming making a full stack application seems very complicated to me because one thing depends on the other and the other thing depends on something else. I am searching for a resource for learning fastapi like a django-book Django by Examples by antonio mele. Building real-world production grade projects.

  • @wait4015
    @wait4015 3 месяца назад

    Could you make a video on how to use dependency injection without fastapi?

    • @codingwithroby
      @codingwithroby  3 месяца назад

      Fairly similar - easy way is to initialize the object you will need in the class and then call directly.
      class FrameworkClass():
      def __init__(self, func):
      self.func = func
      def do_the_job(self):
      # some stuff
      self.func()
      def my_func():
      # do whatever here

  • @CodePythonPro
    @CodePythonPro 3 месяца назад

    Great video Eric, I hope to built videos like this soon (to help python community in Spanish), thanks for sharing your knowledge.

    • @codingwithroby
      @codingwithroby  3 месяца назад +1

      You can do it 🙂 Just be ready for a ton of work and a lot of trial and error.

  • @ConfirmedPlayer
    @ConfirmedPlayer Месяц назад

    Why can't you just declare the class outside the function (router)? It will be initialized only once, like with dependency injection. I still don't realize the difference.

  • @moinundin1221
    @moinundin1221 3 месяца назад +1

    easy concept for spring boot dev

    • @codingwithroby
      @codingwithroby  3 месяца назад +1

      Dependency injection is all over the place in Spring Boot!

  • @ankitmoura1467
    @ankitmoura1467 3 месяца назад

    ok so whole point is to save resources and not create new instance on every api call ryt? but like when we create a new function which returns the new instance for us won't that fn be called on every api call and return a new instance i might be wrong but it feels like we r doing same thing with extra steps help?. also great content

    • @yashasmn245
      @yashasmn245 3 месяца назад

      Point was modular codebase, if you don't keep seperate function you would copy paste it every where .
      Makes easy to understand for everyone.

    • @codingwithroby
      @codingwithroby  3 месяца назад

      It's more for scalable and modular codebase, not resource management.

  • @shafiq_ramli
    @shafiq_ramli 3 месяца назад

    For once I thought DI is a security vulnerability like sqli 😅

  • @aryaa4696
    @aryaa4696 2 месяца назад

    😱8 decade of experience 😅

  • @osogrande4999
    @osogrande4999 2 месяца назад +3

    Nonsense examples. None of those have any real world benefit or even meaning. The whole point of dependency injection in fastapi is to have your dependency function examine, extract or transform any part of the request, since your annotated dependency function arguments can specify parts of the request or the whole request object that fastapi will inject for you when invoking your dependency function, which itself can return a value to inject into your endpoint (or not). These examples literally have zero use in a fastapi endpoint and miss the point 100%.