Advanced DI Patterns & Recipes
Prerequisites
Table of Contents
Interface-Based DI
Creating Interfaces with DI.createInterface
Default Registration
Override Default Implementation
Real Example: Fetch Function Interface
Aliasing Interfaces
Registration Patterns
Registration.instance
Registration.singleton
Registration.transient
Registration.callback
Registration.cachedCallback
Registration.aliasTo
Resolver Patterns
lazy: Deferred Resolution
optional: Graceful Fallbacks
all: Collect All Registrations
factory: Dynamic Instance Creation
newInstanceOf: Always New Instance
newInstanceForScope: Scoped Instances
own: Container-Local Resolution
resource & optionalResource: Smart Resolution
Factory Patterns
Service Factory Pattern
Plugin Factory Pattern
Child Containers & Scoping
Creating Child Containers
Inherit Parent Resources
Scoped Service Pattern
Feature Module Pattern
Transformers
Registering Transformers
Multiple Transformers
Real-World: Adding Lifecycle Hooks
Real-World Recipes
Recipe 1: Multi-Tenant Application
Recipe 2: Environment-Based Configuration
Recipe 3: Plugin System with DI
Recipe 4: Decorator Pattern with Transformers
Recipe 5: Context-Aware Services
Best Practices
Common Pitfalls
Pitfall 1: Circular Dependencies
Pitfall 2: Forgetting Child Container Disposal
Pitfall 3: Over-using Transformers
Conclusion
Last updated
Was this helpful?