Clean IoC
Clean IoC is a dependency injection container for Python that keeps application code framework-agnostic.
It focuses on:
- constructor/factory injection using type hints
- explicit registration and resolution
- lifecycle control with
transient,once_per_graph,scoped,singleton - filtering, decorators, and generic registration helpers
Install
FastAPI integration:
Quick Start
from clean_ioc import Container
class UserRepository:
def get_user(self, user_id: str) -> dict:
return {"id": user_id, "name": "Ada"}
class UserService:
def __init__(self, repo: UserRepository):
self.repo = repo
def get_user(self, user_id: str) -> dict:
return self.repo.get_user(user_id)
container = Container()
container.register(UserRepository)
container.register(UserService)
service = container.resolve(UserService)
print(service.get_user("123"))
Core Concepts
- Register dependencies on
Container/Scope. - Resolve by type.
- Let the container recursively resolve child dependencies.
- Control reuse with lifespans.