Getting started
Register services once. Resolve typed dependencies.
Injex uses regular Python type hints. Application classes do not need base classes, constructor decorators, or framework-specific dependency hooks.
1
Install
Use the PyPI package in any Python 3.10+ project.
2
Register
Describe the service graph at the composition root.
3
Validate
Catch missing dependencies before startup code constructs real services.
Install
pip install injex
Minimal example
from injex import Container
class Repository:
def save(self, email: str) -> int:
return 42
class RegisterUser:
def __init__(self, repository: Repository):
self.repository = repository
def execute(self, email: str) -> int:
return self.repository.save(email)
container = Container()
container.add_singleton(Repository)
container.add_transient(RegisterUser)
container.assert_valid()
use_case = container.resolve(RegisterUser)
Lifetimes
| Method | Meaning | Common use |
|---|---|---|
add_singleton() | One instance reused for the app lifetime. | Settings, clients, repositories. |
add_transient() | New instance on each resolve. | Use cases and commands. |
add_scoped() | One instance per scope. | Request, job, or message state. |
Keep registration code close to application startup. Treat it as your composition root, not as business logic.