Compatiable with any javascript library.
- Add a list of service classes as dependencies
- Easy to fetch the current instance of any dependency
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...
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;
})
import NodeDIManager from "../NodeDIManager.js";
export default class UserController {
#userService = NodeDIManager.get('UserService');
constructor(){}
getClientUsers() {
return this.#userService.getAllUsers();
}
}
import NodeDIManager from "../NodeDIManager.js";
export default class UserService {
#userRepo = NodeDIManager.get('UserRepository');
constructor(){
}
getAllUsers() {
return this.#userRepo.getUsers();
}
}
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'
}
]
}
}