@csejtei/simple-container
v1.0.3
Published
Simple dependency injection container
Downloads
1
Readme
Simple Dependency Injection Container
Install
npm i @csejtei/simple-container
Usage
Use container in your controllers:
- pass a container through the constructor
- get any injectable item (= dependency) (e.g.: a service) from the container
- use injected service anywhere in the controller
Imagine the following example: You have a UserController which is using your custom UserService service, you can inject userService with the Container
import { Container } from '@csejtei/simple-container';
import UserService from '../services/UserService';
class UserController {
userService: UserService;
constructor(container: Container) {
this.userService = container.get(UserService);
}
generateUserHash(salt) {
return this.userService.generateHash(salt);
}
}
Your UserService has to implement Injectable:
class UserService implement Injectable {
// generateHash ...etc
}