Guide

Deep dive into dependency-injector's features and advanced usage patterns.

Scoped Containers

Scoped containers allow you to create hierarchical dependency trees. Child scopes inherit all services from their parent but can also register their own services or override parent services.

Common Use Cases

  • Request-scoped services - Create a scope per HTTP request with request-specific context
  • Testing - Override production services with mocks in test scopes
  • Multi-tenancy - Create tenant-specific scopes with tenant configuration

Service Overrides

Child scopes can override services from parent scopes. This is particularly useful for testing, where you want to replace real implementations with mocks.

Thread Safety

Dependency Injector is designed to be fully thread-safe. All containers can be safely shared across threads using Arc. The internal storage uses DashMap for lock-free concurrent access.

Performance Tip

For hot paths where you resolve the same service frequently, consider caching the Arc<T> reference instead of calling get() repeatedly.

Error Handling

Service resolution returns a Result type. The main error cases are:

  • DiError::NotFound - Service type not registered in container or parent scopes
  • DiError::FactoryPanicked - Factory function panicked during creation