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

preview_player
Показать описание
Learning dependency injection can be hard. In this simple FastAPI tutorial we will go over dependency injection and that Depends Keyword.

🎓 Courses can be found:

👨‍💻 Hi, I am Eric!👨‍💻
Hey! I’m a seasoned developer and Cloud enthusiast. I love sharing my knowledge and helping others to discover the exciting world of programming and cloud technologies. For more tutorials, tips, and tech magic, don’t forget to hit that subscribe button and ring the bell for notifications!

🙏 Support The Channel 🙏
If you found this tutorial helpful, please give it a thumbs up 👍, comment, and share it with your network 🌍

#fastapi #dependencyinjection #python
Рекомендации по теме
Комментарии
Автор

but its just a function calling? what diffrence will it make if i call those function that return objects with in the main function, whats the big diffrence?

tarangsuri
Автор

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

calebmatthias
Автор

Nice video, but I think the pattern here is IOC(inversion of control)

ssswdon
Автор

Hi Eric! I have a question for clarification - so if our function (or class) depends on some object that's already created (like Logging) - we should use DI, but if we need to create a new object every time (e.g. some context manager) - we would use composition (pass a class as an argument and then create new object every time) - is this correct?

inhbtnt
Автор

Well explained and thanks for the detailed explanation.

Please between using a decorator function and dependency injection which would you recommend for user authorization?

mantle_bearer
Автор

If we create an object in another module and import it and use it then will be efficient than DI?

vikrantwangal
Автор

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

CodePythonPro
Автор

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.

ConfirmedPlayer
Автор

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.

BritScientist
Автор

which theme are you using in this tutorial?

farazahmed
Автор

Maravilha!! Ficou muito claro com exemplos práticos! Parabens!

hipolitoribeirocavalcantej
Автор

still don't see the point why moving the instantiation onto input param is a better thing

AGunzOw
Автор

More of these please lol just subbed great explanation

Wolfci
Автор

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

codingwithroby
Автор

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

ankitmoura
Автор

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

wait
Автор

Beginner here. Trying to understand how it's different from just doing a "def log_message(message, logger=get_logger())"

dcgrc
Автор

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

shafiq_ramli
Автор

Start from 4:49, before it's just bullshit to make video longer

ZumaTech
Автор

Hey Eric, thanks for the awesome lesson! we encountered an issue with dependency injection naming (?) :
"EmailResponse = Annotated[EmailService, Depends(get_email_service)]" -> WORKS,
email_response_dependency = Annotated[EmailService, Depends(get_email_service)] -> ERROR:

AI Response which we could use feedback on as you don't seem to have this issue:
" It fails with "Variable not allowed in type expression" because Python's type system interprets names with lowercase and underscores as variables, not as type names. In type annotations, only proper type names (typically PascalCase) are allowed as aliases for complex types.
FastAPI's dependency injection system relies on Python's typing module, which follows these naming conventions strictly. Type aliases should follow class naming conventions (PascalCase), while variables use snake_case."

awakenwithoutcoffee
welcome to shbcf.ru