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

Recipe Index

FastAPI Integration

Recipe

Description

Basic Setup

Container + lifespan integration

Dependency Injection

Using Depends() with dioxide

Testing Endpoints

TestClient with fakes

Middleware Integration

Access container in middleware

Testing Patterns

Recipe

Description

Container Fixture

Fresh container per test

Typed Fake Access

IDE-friendly fixture typing

Async Testing

pytest-asyncio patterns

Error Injection

Configurable fake failures

Time Control

Fake clock for time-dependent tests

Configuration

Recipe

Description

Pydantic Settings

Type-safe config with validation

Profile-Based Config

Different settings per environment

Secrets Management

Secure secret handling

Config Validation

Fail fast on missing config

Database Patterns

Recipe

Description

SQLAlchemy Adapter

Async SQLAlchemy with lifecycle

Repository Pattern

Clean data access abstraction

In-Memory Fake

Fast fake for testing

Transaction Handling

Commit/rollback patterns

Library Authors

Recipe

Description

dioxide-Compatible Libraries

Optional DI with sensible defaults

Philosophy

These recipes follow dioxide’s core principles:

  1. Fakes over mocks - Real implementations with shortcuts

  2. Ports define boundaries - Clear interfaces between layers

  3. Profiles select adapters - Environment determines implementation

  4. Type safety matters - Leverage Python’s type system