Skip to main content

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
Before learning FastAPI’s Depends(), it’s important to understand three related concepts:
  • Dependency
  • Dependency Injection (DI)
  • Inversion of Control (IoC)
Although these concepts are closely related, they are not the same.

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
Simply put,
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:
With Dependency Injection:
The service no longer creates its own dependency. Instead, the dependency is supplied from outside. This makes the code:
  • Loosely coupled
  • Easier to test
  • Easier to reuse
  • Easier to maintain

FastAPI Already Performs Injection

Even before using Depends(), FastAPI is already injecting values into your endpoint functions.
Request:
FastAPI automatically injects: Conceptually, FastAPI performs something similar to:
You never write this extraction code yourself. FastAPI supplies the required values automatically. This introduces the idea of injection. Later, FastAPI extends this same mechanism to inject application resources using 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:
With IoC:
IoC answers:
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:
Frameworks such as FastAPI, Spring, and ASP.NET Core use both concepts together.

Real-World Analogy

Imagine a chef working in a restaurant. Without IoC: The chef must:
  • Buy vegetables
  • Arrange utensils
  • Prepare ingredients
  • Cook
With IoC: Restaurant management prepares everything. The chef only cooks. 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 through Depends().
When a request arrives, FastAPI:
  1. Resolves the dependency.
  2. Injects it into the endpoint.
  3. Cleans it up after the request (if required).
The endpoint simply receives what it needs. FastAPI commonly injects:
  • 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

No.
  • IoC is a design principle.
  • DI is a design pattern.
IoC answers who controls the application, while DI answers how dependencies are provided.
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.
Yes.Example:
You created the dependency yourself and injected it manually.This is Dependency Injection without IoC because your application still controls object creation.
Yes.A framework can control object creation and application flow using callbacks, event handlers, plugins, or other techniques without using Dependency Injection.
No.Dependency Injection is only about supplying dependencies.Who creates those dependencies depends on the application or framework.
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
Creating and managing objects is part of FastAPI’s IoC behavior, while supplying them to your code is Dependency Injection.
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.
Dependency Injection helps build applications that are:
  • Loosely coupled
  • Easier to test
  • More reusable
  • Easier to maintain
  • Easier to extend
Both use the same idea—FastAPI supplies what your endpoint needs.Request Data Injection provides values extracted from the HTTP request:
  • Path Parameters
  • Query Parameters
  • Headers
  • Cookies
  • Request Body
Dependency Injection with Depends() provides reusable application resources:
  • Database Sessions
  • Services
  • Repositories
  • Current User
  • Configuration
The difference is what is being injected, not how it is injected.