This is a step-by-step guide to learn Dependency Injection (DI) in Python. Each section has working code examples that you can run and experiment with.
-
Why Dependency Injection?
- Problems with tightly coupled code
- How DI makes code better
- Simple database example
-
Basic Patterns
- What is Dependency Injection?
- Real example with notification system
- Different ways to inject dependencies
-
IoC Principles
- What is Inversion of Control?
- Event-based systems
- How frameworks use IoC
-
DI Container
- What is a DI container?
- How to register services
- How to resolve dependencies
-
Web Application
- Real example using Litestar
- How to structure a web app with DI
- How to test code with DI
- Start from section 1 and move forward
- Each folder has:
- Code examples you can run
- README explaining the concepts
- Problems and solutions
- Python 3.8 or newer
- Basic Python knowledge
- For web app example:
pip install litestar ...
Note: Check out
my_brain_dump.py- my first attempt at building a DI container. It might be weird, but "it's nice" 😄