Dependency Injection Framework
Relevant source files
The following files were used as context for generating this wiki page:
The Dependency Injection (DI) Framework in MarketSage is a centralized system designed to manage the lifecycle, registration, and resolution of services throughout the application. It promotes loose coupling between components by allowing dependencies to be injected rather than hard-coded, facilitating easier testing and modularity.
The framework utilizes a singleton container to store service instances and provides TypeScript decorators to simplify the registration and retrieval process. This architecture ensures that core services like the SmartShoppingAssistant and SocialCommerceAnalytics can access their required dependencies without manual instantiation.
Sources: src/core/di/container.ts, src/core/di/decorators.ts:5-25, src/core/ai/SmartShoppingAssistant.ts:98-101
Core Architecture and Components
The DI system is built around a central Container that acts as a registry for all application services. It supports both class-based registration and property-based injection.
DIContainer
The DIContainer is the primary engine of the framework. It follows the Singleton pattern to ensure a single source of truth for all services within the runtime environment. It provides methods to register constructors and get resolved instances.
Sources: src/core/di/container.ts, src/core/di/decorators.ts:13-15
Service Decorators
The framework provides two primary decorators to interact with the container:
- @Service(name?: string): Applied to classes to automatically register them with the
DIContainer. If no name is provided, the class name is used as the identifier. - @Inject(serviceName: string): Applied to class properties to lazily resolve and inject the requested service instance from the container at runtime.
Sources: src/core/di/decorators.ts:10-33
Registration and Resolution Flow
The following diagram illustrates how a service is registered and subsequently injected into a dependent component:
The DIContainer ensures that services are registered before they are requested, typically during the module loading phase via decorators.
Sources: src/core/di/decorators.ts:11-25
Framework Implementation Details
Decorator Logic
The @Inject decorator modifies the target property by defining a custom getter. This getter calls the DIContainer.getInstance().get(serviceName) method, ensuring that the dependency is resolved only when accessed.
Sources: src/core/di/decorators.ts:24-32
| Component | Type | Responsibility |
|---|---|---|
@Service | Class Decorator | Registers a class into the DIContainer with an optional alias. |
@Inject | Property Decorator | Links a property to a service instance stored in the container. |
DIContainer | Singleton Class | Manages the internal map of service names to instances/constructors. |
Service | Alias Decorator | Alternative name for @Service often imported from ServiceContainer. |
Sources: src/core/di/decorators.ts:8-35, src/core/di/ServiceContainer.ts
Service Integration Example
The SmartShoppingAssistant utilizes the DI framework to register itself as a service, allowing other parts of the system to inject it.
@Service('SmartShoppingAssistant')
export class SmartShoppingAssistant {
constructor(
pricePrediction: PricePredictionEngine,
threatDetection: AdvancedThreatDetection
) {
this.pricePrediction = pricePrediction;
this.threatDetection = threatDetection;
}
}
Sources: src/core/ai/SmartShoppingAssistant.ts:98-111
Error Handling in Services
The framework also provides an @AsyncErrorHandler decorator, which is frequently used alongside DI-managed services to wrap asynchronous methods in consistent error-handling logic.
This ensures that services registered via the DI framework maintain high availability and provide descriptive error traces.
Sources: src/core/di/decorators.ts:39-55
Summary
The Dependency Injection Framework in MarketSage provides a robust infrastructure for managing complex service dependencies. By utilizing TypeScript decorators like @Service and @Inject, the project achieves a clean separation of concerns, allowing core logic in SmartShoppingAssistant and SocialCommerceAnalytics to remain focused on their primary tasks while the framework handles object instantiation and lifecycle management.
Sources: src/core/di/decorators.ts, src/core/ai/SmartShoppingAssistant.ts, src/core/social/SocialCommerceAnalytics.ts