Cookbook¶
Practical, copy-paste-ready recipes for common dioxide patterns.
This cookbook provides working code examples for real-world scenarios. Each recipe follows a consistent format:
Problem: What you’re trying to solve
Solution: The dioxide approach
Code: Complete, working example
Explanation: Why this works
Quick Links¶
Routes, dependency injection, lifecycle management, and testing endpoints.
Fixtures, fakes, test isolation, and async testing patterns.
Pydantic Settings integration and environment-specific config.
SQLAlchemy adapters, repository pattern, and transaction handling.
Make your library dioxide-compatible without depending on dioxide.
Recipe Index¶
FastAPI Integration¶
Recipe |
Description |
|---|---|
Container + lifespan integration |
|
Using |
|
TestClient with fakes |
|
Access container in middleware |
Testing Patterns¶
Recipe |
Description |
|---|---|
Fresh container per test |
|
IDE-friendly fixture typing |
|
pytest-asyncio patterns |
|
Configurable fake failures |
|
Fake clock for time-dependent tests |
Configuration¶
Recipe |
Description |
|---|---|
Type-safe config with validation |
|
Different settings per environment |
|
Secure secret handling |
|
Fail fast on missing config |
Database Patterns¶
Recipe |
Description |
|---|---|
Async SQLAlchemy with lifecycle |
|
Clean data access abstraction |
|
Fast fake for testing |
|
Commit/rollback patterns |
Philosophy¶
These recipes follow dioxide’s core principles:
Fakes over mocks - Real implementations with shortcuts
Ports define boundaries - Clear interfaces between layers
Profiles select adapters - Environment determines implementation
Type safety matters - Leverage Python’s type system