node-di-manager
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

Node DI Manager

Light Weight, Easy To Use, Dependency Injection Container

Build Status

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'
            }
        ]
    }
}

Package Sidebar

Install

npm i node-di-manager

Weekly Downloads

2

Version

1.1.6

License

ISC

Unpacked Size

8.91 kB

Total Files

5

Last publish

Collaborators

  • vvkkumar06