node-di-manager
v1.1.6
Published
Light weight easy to use, node dependency injection container
Downloads
18
Maintainers
Readme
Node DI Manager
Light Weight, Easy To Use, Dependency Injection Container
Compatiable with any javascript library.
Features
- Add a list of service classes as dependencies
- Easy to fetch the current instance of any dependency
Installation
Node DI Manager requires Node.js v10+ to run.
Install the dependencies and devDependencies and start the server.
npm i node-di-manager
For production environments...
Development
index.js
async function configure() {
let map = new Map();
map.set(path.join(__dirname,'repositories/user-repo.js'), 'UserRepository');
map.set(path.join(__dirname,'services/user-service.js'), 'UserService')
return await NodeDIManager.register(map);
}
configure()
.then((data) => {
// Data is the list of dependencies initialized
let userController = new UserController();
console.log(userController.getClientUsers());
})
.catch(err => {
throw err;
})
UserController.js
import NodeDIManager from "../NodeDIManager.js";
export default class UserController {
#userService = NodeDIManager.get('UserService');
constructor(){}
getClientUsers() {
return this.#userService.getAllUsers();
}
}
UserService.js
import NodeDIManager from "../NodeDIManager.js";
export default class UserService {
#userRepo = NodeDIManager.get('UserRepository');
constructor(){
}
getAllUsers() {
return this.#userRepo.getUsers();
}
}
UserRepository.js
export default class UserRepository {
constructor(){}
getUserById(id){
return {
name: 'Vivek',
designation: 'Developer'
}
};
getUsers() {
return [
{
name: 'Sam',
designation: 'Developer'
},
{
name: 'Devid',
designation: 'Developer'
},
{
name: 'Vivek',
designation: 'Developer'
}
]
}
}