Module 11 ยท Routers & Dependency Injection โ Lesson 2 of 4 ยท ~6 min
Dependency injection
Depends() hands each request what it needs
Every route above took a session: Session = Depends(get_session) parameter. Depends(...) is FastAPI's dependency injection: instead of the route function reaching out and creating its own database session, FastAPI calls get_session() for you, per request, and hands the result in as an argument. Same idea for the logged-in user: Depends(get_current_walker).
Compare this to Part I's APIClient: your SwiftUI views held a reference to a shared APIClient instance (often via @EnvironmentObject or a singleton) and called its methods directly. FastAPI's version is more automatic โ you just declare what type of thing you need as a parameter, and Depends(...) says how to get one. The route never constructs it itself.