Dependency Injection API
Vercube provides a powerful and flexible dependency injection (DI) system that helps manage dependencies and promote loose coupling in your application. This documentation covers the complete API of the @vercube/di
package.
Install
The easiest way to get started with Vercube DI is to use install package:
$ pnpm add @vercube/di
$ npm i @vercube/di
$ yarn add @vercube/di
$ bun add @vercube/di
Overview
The dependency injection system in Vercube consists of several key components:
- Container: The central registry that manages all dependencies and their lifecycle
- Decorators: A set of decorators for declaring and injecting dependencies
- Service Registration: Methods for registering services with different scopes
- Dependency Resolution: Automatic resolution of dependencies when needed
Table of Contents
- Container - The core container class and its methods
- Decorators - Available decorators for dependency injection
- Types - Type definitions used in the DI system
- Advanced Topics - Advanced usage patterns and techniques
Quick Start
import { Container, Inject } from '@vercube/di';
// Create a container
const container = new Container();
// Register a service
container.bind(Logger, ConsoleLogger);
// Use dependency injection in a class
class UserService {
@Inject(Logger)
private logger!: Logger;
async getUser(id: string) {
this.logger.info(`Fetching user ${id}`);
// ...
}
}
// Resolve a service with its dependencies
const userService = container.resolve(UserService);
Core Concepts
Container
The Container is the heart of the dependency injection system. It manages the registration and resolution of dependencies. See the Container documentation for details.
Service Registration
Services can be registered in different ways:
- Singleton: One instance shared across the application
- Transient: New instance for each request
- Instance: An existing instance
- Mock: A mock implementation for testing
Dependency Injection
Dependencies are injected using decorators:
@Inject
: Injects a required dependency@InjectOptional
: Injects an optional dependency@Init
: Marks a method to be called during initialization
Service Resolution
Services can be resolved in different ways:
container.get()
: Get a service instancecontainer.getOptional()
: Get an optional service instancecontainer.resolve()
: Create a new instance with dependencies automatically resolved
Best Practices
Use Constructor Injection When Possible
typescriptclass UserController { constructor( @Inject(UserService) private userService: UserService, @Inject(Logger) private logger: Logger ) {} }
Register Services Early
typescript// In your application bootstrap container.bind(Logger, ConsoleLogger); container.bind(UserService); container.bind(UserController);
Use Interfaces for Better Abstraction
typescriptinterface ILogger { info(message: string): void; error(message: string): void; } class UserService { @Inject(ILogger) private logger!: ILogger; }
Keep Services Focused
typescript// Good class UserService { @Inject(UserRepository) private userRepository!: UserRepository; } // Bad - too many dependencies class UserService { @Inject(UserRepository) private userRepository!: UserRepository; @Inject(EmailService) private emailService!: EmailService; @Inject(PaymentService) private paymentService!: PaymentService; @Inject(NotificationService) private notificationService!: NotificationService; }
See Also
- Dependency Injection Guide - Conceptual overview of DI in Vercube
- Decorators - Available decorators for dependency injection
- Types - Type definitions used in the DI system
- Advanced Topics - Advanced usage patterns and techniques