FastIoC
IoC/DI container for FastAPI with automatic type-based dependency injection
Why FastIoC 🤔
FastIoC bridges the gap between Python’s dynamic nature and modern dependency injection patterns found in frameworks like .NET, Laravel, Spring Boot, and NestJS — with zero boilerplate and full FastAPI compatibility. It’s designed to make implementing scalable architectural patterns such as Clean Architecture or Hexagonal Architecture effortless and intuitive.
Features:
-
🧹 Write cleaner, loosely coupled code while staying true to the ⛓️💥 Dependency Inversion Principle (SOLID - D) — with ABSOLUTELY ZERO boilerplate! ⚡
-
⚙️ Enjoy hassle-free, automatic nested dependency resolution using Python type hints with flexible lifetimes: ♻️ Singleton, 🧺 Scoped, and ♨️ Transient (inspired by .NET)
-
🚀 Zero runtime overhead — everything is resolved at startup!
-
🤝 100% compatible & based on FastAPI’s native dependency injection — no black boxes, no magic 🪄
-
♻️ Singleton support with automatic cleanup on application shutdown 🧹
-
🧪 Full support for FastAPI's
dependency_overridesusing type annotations — even with mock containers 💉 -
📦 Comes with the amazing
APIController— the best class-based view (CBV) system ever seen in Python 🏆 -
🔧 Comes with customizable hooks, detailed logs & ... 📊
Sponsors 💝
You can us on a regular basis to become a sponsor. For more info, contact OpenMindAmir@gmail.com.
Installation 📥
$ pip install fastioc
Usage 💡
Sample interface & implementation:
from typing import Protocol
# Define the interfaces 📜
class INumberGenerator(protocol):
def generate(self) -> int: ...
class IService(Protocol):
def get_number(self) -> int: ...
# Implement concrete classes (Actual dependencies) 🏗️
class SimpleNumberGenerator(INumberGenerator):
def generate(self) -> int:
return 42
class ExampleService(IService):
number_service: INumberGenerator # Nested dependency with type hints! ⚡
def get_number(self) -> int:
return self.number_service.generate()
Create container, register dependencies & use them in endpoints!
from fastapi import FastAPI
from fastioc import Container # Import the Container
# Create container and register dependency 📝
container = Container()
container.add_scoped(INumberGenerator, SimpleNumberGenetator)
container.add_scoped(IService, ExampleService) # Also available: add_singleton, add_transient
# Create FastAPI app and integrate it with the container 🪄
app = FastAPI()
container.injectify(app)
# Now your endpoints are injectified! 🎉
@app.get('/')
def index(service: IService) -> int: # Only use the interface - no 'Depends' needed
return service.get_number() # 42 🤩
APIController 📦
from fastapi import FastAPI
from fastioc import Container
from fastioc.controller import APIController, get, post
# Create container & register dependencies 📝
container = Container()
container.add_scoped(IService, ExampleService)
# Define an example controller
class ExampleController(APIController):
config = { # APIRouter parameters (+ IDE Autocomplete 🤩)
"prefix": '/example',
"tag": 'example',
"container": container # ! DO NOT FORGET
}
service: IService # Available in all endpoints! ⚡
@get('/read')
def read_example(self) -> int:
return self.service.get_number()
@post('/set')
def set_example(self) -> bool:
# ...
return True
app = FastAPI()
app.include_router(ExampleController.router()) # Get router from controller and include it
- APIController endpoints are injectified so you can also resolve dependencies in each endpoint separately.
- You can also resolve dependencies in
__init__of your controller. - Read more in the APIController documentation
Learn More 📘
Check out the full documentation for advanced examples, architecture guides, best practices, and more.
Contributing 💬
Got an idea, found a bug, or want to improve FastIoC?
Feel free to open an issue or submit a pull request — contributions are always welcome 🤝
License ⚖️
This project is licensed under the MIT License — see the LICENSE file for details.
Release files for FastIoC 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastioc-0.2.0.tar.gz | 22.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastioc-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.1 kB
Release files / fastioc-0.2.0.tar.gz
| Download URL | fastioc-0.2.0.tar.gz |
|---|---|
| Size | 22.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3761df7b264a4118a167a9b996af207dabdca68d89f4c14dfb530b34aa54e7a5
|
|
BLAKE2b-256 checksum How to use checksums |
df9dd32db5fc8b4cb9d79e1e2a39c07cc041fd6eb75fdf8201d5fa26fbebbe94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.3
|
Release files / fastioc-0.2.0-py3-none-any.whl
| Download URL | fastioc-0.2.0-py3-none-any.whl |
|---|---|
| Size | 22.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4dddc06dbb13b7641fc6d701bbeac809dda927db37df7b4a2d7652598abc7d0f
|
|
BLAKE2b-256 checksum How to use checksums |
f66f847139c98a193fae636103a68be1856d73c6f4e7a2c670ef02684a91323f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.3
|