@wroud/di

@wroud/di is a lightweight dependency injection library for JavaScript, inspired by .NET's DI system. Written in TypeScript, it supports modern JavaScript features, including decorators, and provides robust dependency management capabilities.
Features
- Modern JavaScript: Leverages ES modules, decorators, and asynchronous service loading for advanced performance optimizations.
- TypeScript: Written in TypeScript for type safety.
- Flexible DI: Supports singleton, transient, and scoped services.
- Pure ESM package
Installation
Install via npm:
npm install @wroud/di
Install via yarn
yarn add @wroud/di
Documentation
For detailed usage and API reference, visit the documentation site.
Example
import {
createService,
injectable,
ServiceContainerBuilder,
} from "@wroud/di";
interface ILogger {
log: (message: string) => void;
}
interface IAuthService {
authenticate(user: string, password: string): void;
}
const ILogger = createService<ILogger>("ILogger");
const IAuthService = createService<IAuthService>("IAuthService");
function loggerFactory(): ILogger {
return {
log: (message: string) => console.log(message),
};
}
@injectable(() => [ILogger])
class AuthService implements IAuthService {
constructor(private readonly logger: ILogger) {}
authenticate(user: string, password: string): void {
this.logger.log(`Authenticating user ${user}`);
}
}
const provider = new ServiceContainerBuilder()
.addSingleton(ILogger, loggerFactory)
.addSingleton(IAuthService, AuthService)
.build();
const authService = provider.getService(IAuthService);
await authService.authenticate("user1", "password");
Changelog
All notable changes to this project will be documented in the CHANGELOG file.
License
This project is licensed under the MIT License. See the LICENSE file for details.