Visual Diagrams
Table of Contents
1. Container Hierarchy and Resolution
ROOT CONTAINER (Application Level)
══════════════════════════════════════════════════════
┌────────────────────────────────────────────────┐
│ Root Container │
│ │
│ Registrations: │
│ • ILogger → ConsoleLogger (singleton) │
│ • IAppConfig → {...} (instance) │
│ • IApiClient → ApiClient (singleton) │
└────────────────────┬───────────────────────────┘
│
┌──────────┴──────────┐
↓ ↓
┌─────────────────────┐ ┌──────────────────────┐
│ Feature Container 1 │ │ Feature Container 2 │
│ (Admin Module) │ │ (User Module) │
│ │ │ │
│ Inherits: │ │ Inherits: │
│ • ILogger ✓ │ │ • ILogger ✓ │
│ • IAppConfig ✓ │ │ • IAppConfig ✓ │
│ • IApiClient ✓ │ │ • IApiClient ✓ │
│ │ │ │
│ Overrides: │ │ Adds: │
│ • ILogger → │ │ • IUserService → │
│ AdminLogger │ │ UserService │
│ │ │ • IAuthGuard → │
│ Adds: │ │ UserAuthGuard │
│ • IAdminService → │ │ │
│ AdminService │ │ │
└─────────────────────┘ └──────────────────────┘
↓ ↓
┌─────────┐ ┌──────────┐
│Component│ │Component │
│ asks │ │ asks │
│ for │ │ for │
│ ILogger │ │ ILogger │
└────┬────┘ └────┬─────┘
│ │
↓ ↓
AdminLogger ConsoleLogger
(from Feature 1) (from Root)
RESOLUTION ALGORITHM
════════════════════════════════════════════════════════
Component requests: IUserService
Step 1: Check current container
┌────────────────────────────────────┐
│ Current: Feature Container 2 │
│ Has IUserService? YES ✓ │
│ → Return UserService instance │
└────────────────────────────────────┘
Resolution complete!
Component requests: IApiClient
Step 1: Check current container
┌────────────────────────────────────┐
│ Current: Feature Container 2 │
│ Has IApiClient? NO ✗ │
└──────────────┬─────────────────────┘
↓
Step 2: Check parent container
┌────────────────────────────────────┐
│ Parent: Root Container │
│ Has IApiClient? YES ✓ │
│ → Return ApiClient instance │
└────────────────────────────────────┘
Resolution complete!
Component requests: IUnknownService
Step 1: Check current container
┌────────────────────────────────────┐
│ Current: Feature Container │
│ Has IUnknownService? NO ✗ │
└──────────────┬─────────────────────┘
↓
Step 2: Check parent container
┌────────────────────────────────────┐
│ Parent: Root Container │
│ Has IUnknownService? NO ✗ │
└──────────────┬─────────────────────┘
↓
Step 3: No more parents
┌────────────────────────────────────┐
│ ❌ ERROR: Cannot resolve key │
│ IUnknownService │
└────────────────────────────────────┘
CREATING CHILD CONTAINERS
════════════════════════════════════════════════════════
const root = DI.createContainer();
root.register(
Registration.singleton(ILogger, ConsoleLogger),
Registration.instance(IConfig, appConfig)
);
const feature = root.createChild();
feature.register(
// Override logger for this feature
Registration.singleton(ILogger, FeatureLogger),
// Add feature-specific service
Registration.singleton(IFeatureService, FeatureService)
);
// Resolution:
root.get(ILogger); // → ConsoleLogger
feature.get(ILogger); // → FeatureLogger (override)
feature.get(IConfig); // → appConfig (inherited from root)
USE CASES FOR CHILD CONTAINERS
════════════════════════════════════════════════════════
1. Feature Modules
├─ Override services for specific features
└─ Isolate feature-specific dependencies
2. Testing
├─ Create test container with mocks
└─ Don't pollute root container
3. Multi-Tenancy
├─ Each tenant gets own container
└─ Override config/services per tenant
4. Component Scoping
├─ Component creates child container
└─ Scoped services destroyed with component2. Service Lifetimes
3. Injection Methods Comparison
4. Registration Flow
5. Resolver Pipeline
6. Property vs Constructor Injection
7. Interface Token Creation
Summary
Last updated
Was this helpful?