This directory contains practical examples demonstrating all features of fastapi-di-kit.
Each example is a standalone Python script that can be run directly:
# From the repository root
uv run python examples/01_basic_usage.pyOr using standard Python:
cd examples
python 01_basic_usage.pyFile: 01_basic_usage.py
Demonstrates:
- Service registration with
@servicedecorator - Automatic dependency injection
- Manual resolution from container
- Singleton lifecycle (default)
Key Concepts:
- How to define services
- How dependencies are automatically resolved based on type hints
- How to use the DI container
File: 02_lifecycle_management.py
Demonstrates:
- SINGLETON lifecycle (one instance globally)
- TRANSIENT lifecycle (new instance every time)
- SCOPED lifecycle (one instance per request)
- Using
setup_di_middlewarefor request-scoped services
Key Concepts:
- Different lifecycle modes and when to use each
- How scoped services work with FastAPI middleware
- Performance implications of different lifecycles
Requires: FastAPI server (runs on http://localhost:8000)
File: 03_lazy_loading.py
Demonstrates:
- Using
Lazy[T]to defer expensive initialization - Performance benefits of lazy loading
- Lazy caching behavior
- When to use lazy loading
Key Concepts:
- Deferring dependency resolution until needed
- Breaking circular dependencies
- Improving startup performance
File: 04_factory_functions.py
Demonstrates:
@factorydecorator for complex initialization- Factory functions with dependencies
- Runtime configuration (environment variables)
- Conditional service creation (dev/prod switching)
Key Concepts:
- When to use factories vs constructors
- Creating services with external dependencies
- Environment-based configuration
File: 05_testing_and_mocking.py
Demonstrates:
- Creating mock implementations
- Isolated test containers
- Overriding services for testing
- pytest integration patterns
Key Concepts:
- How to test services with DI
- Creating and using mocks
- Avoiding global state in tests
File: 06_async_services.py
Demonstrates:
- Async service definitions
- Async methods in services
- Using async services in FastAPI
- Parallel async operations
- Async with different lifecycles
Key Concepts:
- How DI works with async/await
- Async database and API clients
- Request-scoped async services
Requires: FastAPI server (runs on http://localhost:8000)
Directory: hexagonal_app/
Demonstrates:
- Complete hexagonal architecture implementation
- Port/adapter pattern
- Domain-driven design with DI
- Repository pattern with interface binding
Key Concepts:
- Separating domain from infrastructure
- Using
@repositorydecorator - Interface-to-implementation binding
- Clean architecture with fastapi-di-kit
To Run:
cd examples/hexagonal_app
uv run python main.pyRequires: FastAPI server (runs on http://localhost:8000)
- Start Simple: Begin with
01_basic_usage.pyto understand fundamentals - Learn Lifecycles:
02_lifecycle_management.pyis crucial for production apps - Optimize: Use
03_lazy_loading.pypatterns for expensive dependencies - Configure: Apply
04_factory_functions.pypatterns for runtime configuration - Test: Follow
05_testing_and_mocking.pypatterns for testable code - Async: Use
06_async_services.pypatterns for I/O-bound operations
Most examples require:
fastapiuvicorn(for web examples)
Install with:
uv add fastapi uvicornOr:
pip install fastapi uvicornSee the main README for API reference and additional documentation.