FastAPI - Dependency Injection
Why does FastAPI recommend using shared dependencies with
Depends() instead of calling functions directly inside route handlers?Depends() instead of calling functions directly inside route handlers?Depends() allows FastAPI to control when and how dependencies run, including caching results per request and running cleanup code.
Calling functions directly bypasses FastAPI's dependency system, losing benefits like automatic cleanup and proper execution order.
Depends() manages execution order, caching, and cleanup automatically. -> Option A15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions