1.0.2 • Published 4 years ago

@csejtei/simple-dependency-container v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

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
}
1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago