@csejtei/simple-dependency-container
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Simple Dependency Injection Container

typescript nodejs

Install

npm i @csejtei/simple-dependency-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-dependency-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
}

Readme

Keywords

none

Package Sidebar

Install

npm i @csejtei/simple-dependency-container

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

3.15 kB

Total Files

8

Last publish

Collaborators

  • csejtei