Dependency Injection in FastAPI Python | Parameterized Dependency

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

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

  • @Chris-k4j1z
    @Chris-k4j1z Год назад +2

    Great name for your channel. Well done!

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

    Hey man I have been watching your content from your site and I gotta say it's pretty cool. Hope your channel blow up🎉🎉🎉

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

    Great tutorial, thank you!

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

    Great work!. 👏👏👏

  • @uuuummm9
    @uuuummm9 Год назад +4

    There is an old way of overriding something for tests, such as unittest.mock.patch.
    I do not really get the advantage of this new dependency injection approach as it seems to introduce a lot of dependencies in the code. In your example you are sing super simple approach where your endpoints and your database usage are in the same file. However in practice there are much more layers in between. Even more - your endpoint is usually not supposed to know whether one of the underlaying layers is using database or not. And if you use this dependency injection approach you will have to pass the database object from the top hierarchy down to every method. And this is only one database object - think about all the other dependencies on other components (configuration, external services) you may have. Defining them at the top level of your application and carrying over everywhere looks to me a complete disaster for readability at least.
    With "patch" everything is much simpler - you have somewhere a method "get_database" (somewhere means where the database is actually used). And you patch it in your tests. So basically there are two things which know about this database - the functionality that uses the database and the test itself. For everything in between there is no need to know about that database at all. It is my opinion, I may be wrong and it would be great to hear your opinion too.

  • @romanwired
    @romanwired 5 месяцев назад

    The user 9 have a pretty name