Introduction
Dependency Injection (DI) is one of the most important concepts used in production-grade FastAPI applications. It helps build applications that are:- Reusable
- Loosely coupled
- Easier to test
- Easier to maintain
Depends(), it’s important to understand three related concepts:
- Dependency
- Dependency Injection (DI)
- Inversion of Control (IoC)
What is a Dependency?
A dependency is any object, resource, or service that another object needs in order to perform its work. For example, an endpoint may depend on:- Database Session
- Repository
- Service
- Current User
- Configuration
- Logger
- External API Client
A dependency is something your code needs to do its job.
What is Dependency Injection (DI)?
Dependency Injection (DI) is a design pattern where an object or function receives the dependencies it needs from an external source instead of creating them internally. Instead of saying,“I’ll create everything myself.”your code simply says,
“Give me what I need.”DI focuses only on providing (injecting) dependencies. It does not define:
- Who creates them.
- Who manages them.
- Who destroys them.
Simple Python Example
Without Dependency Injection:- Loosely coupled
- Easier to test
- Easier to reuse
- Easier to maintain
FastAPI Already Performs Injection
Even before usingDepends(), FastAPI is already injecting values into your endpoint functions.
Conceptually, FastAPI performs something similar to:
Depends().
What is Inversion of Control (IoC)?
Inversion of Control (IoC) is a design principle where the responsibility of controlling object creation and application flow is delegated to a framework instead of your application. Without IoC:Who controls object creation and application flow?Answer:
The framework.
Relationship Between IoC and DI
Although often used together, they solve different problems.
Think of them like this:
Real-World Analogy
Imagine a chef working in a restaurant. Without IoC: The chef must:- Buy vegetables
- Arrange utensils
- Prepare ingredients
- Cook
The restaurant management controlling the workflow is IoC.
Providing prepared ingredients to the chef is Dependency Injection.
How FastAPI Uses Dependency Injection
FastAPI provides a built-in Dependency Injection system throughDepends().
- Resolves the dependency.
- Injects it into the endpoint.
- Cleans it up after the request (if required).
- Database Sessions
- Repositories
- Services
- Current User
- Authentication
- Configuration
- External Clients
- Logger
Dependency Resolution Flow
Benefits
Dependency Injection helps build applications that are:- Loosely Coupled
- Modular
- Reusable
- Easier to Test
- Easier to Maintain
- Easier to Extend
Summary
Key Takeaways
- A dependency is anything your code needs.
- Dependency Injection means receiving dependencies instead of creating them.
- IoC means the framework controls the application’s workflow and object lifecycle.
- DI and IoC are related but different concepts.
- FastAPI injects request data automatically.
Depends()extends this mechanism to inject reusable application resources.
Frequently Asked Questions
Is Dependency Injection the same as Inversion of Control?
Is Dependency Injection the same as Inversion of Control?
No.
- IoC is a design principle.
- DI is a design pattern.
Is Dependency Injection an implementation of IoC?
Is Dependency Injection an implementation of IoC?
Not exactly.A more accurate statement is:
Dependency Injection is one of the most common techniques used to achieve Inversion of Control.DI and IoC are different concepts that are frequently used together.
Can Dependency Injection exist without IoC?
Can Dependency Injection exist without IoC?
Yes.Example:You created the dependency yourself and injected it manually.This is Dependency Injection without IoC because your application still controls object creation.
Can IoC exist without Dependency Injection?
Can IoC exist without Dependency Injection?
Yes.A framework can control object creation and application flow using callbacks, event handlers, plugins, or other techniques without using Dependency Injection.
Does Dependency Injection create objects?
Does Dependency Injection create objects?
No.Dependency Injection is only about supplying dependencies.Who creates those dependencies depends on the application or framework.
Why does FastAPI create dependencies then?
Why does FastAPI create dependencies then?
Because FastAPI combines IoC and Dependency Injection.FastAPI:
- Resolves dependencies
- Creates objects when needed
- Injects them into your endpoint
- Cleans them up after the request
Is FastAPI already using Dependency Injection before Depends()?
Is FastAPI already using Dependency Injection before Depends()?
Yes.FastAPI already injects request data into endpoint parameters, including:
- Path Parameters
- Query Parameters
- Request Bodies
- Headers
- Cookies
- Form Data
- Uploaded Files
Depends() extends this same mechanism to inject higher-level application resources such as database sessions, repositories, services, and authentication.Why should I use Dependency Injection?
Why should I use Dependency Injection?
Dependency Injection helps build applications that are:
- Loosely coupled
- Easier to test
- More reusable
- Easier to maintain
- Easier to extend
What is the difference between request data injection and Depends() injection?
What is the difference between request data injection and Depends() injection?
Both use the same idea—FastAPI supplies what your endpoint needs.Request Data Injection provides values extracted from the HTTP request:Dependency Injection with The difference is what is being injected, not how it is injected.
- Path Parameters
- Query Parameters
- Headers
- Cookies
- Request Body
Depends() provides reusable application resources:- Database Sessions
- Services
- Repositories
- Current User
- Configuration