@samarth-srivastava/dil
v1.0.1
Published
dil, in hindi/urdu literal meaning heart, is a DI container in typescript nodejs
Downloads
141
Maintainers
Readme
Dil - A simple Dependency Inversion container for javascript/typescript
Dil, in hindi/urdu literal meaning heart, is a DI container.
Register your services once and then resolve them anywhere needed.
Usage/Examples
// services/LoggerService.ts
export class LoggerService {
log(message: string): void {
// your code here
}
}
Another service dependent on logger service
// services/UserService.ts
import { LoggerService } from './LoggerService';
export class UserService {
constructor(private logger: LoggerService) {}
getUser() {
this.logger.log(/* log something*/);
// return user;
}
}
Register both the services with Dil container
// app.ts
import { container } from '@samarth-srivastava/dil';
import { LoggerService } from './services/LoggerService';
import { UserService } from './services/UserService';
// Register services
container.register<LoggerService>('loggerService', new LoggerService());
container.register<UserService>('userService', new UserService(container.resolve<LoggerService>("loggerService")));
// Resolve the userService and use it
const userService = container.resolve<UserService>('userService');
const user = userService.getUser();