injects
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Injects

Dependency injection for TypeScript : https://mickgrz.github.io/injects/

Use case :

Use a class instance without instantiate it.

The responsibility of providing dependency is delegated to an injector.

This separates the responsibilities of usages and construction.

This also improves readability, testability & maintainability.

Example :

class Dao {
    public create(entity: any) {
        [...]
    }
}
import {inject} from 'injects';
import Dao from './Dao';
 
class Service {
 
  private dao: Dao;
 
  constructor() {
    this.dao = inject(Dao);
  }
 
  public create(entity: any) {
    return this.dao.create(entity);
  }
}
const service: Service = new Service();
service.create({ entityId: 1 });// will call dao.create

You can also write :

import {inject} from 'injects';
import Dao from './Dao';
 
class Service {
 
  private dao: Dao = inject(Dao);
 
  public create(entity: any) {
    return this.dao.create(entity);
  }
}

Unit test example :

Stubbing dependency is easy :

import {Injectable} from 'injects';
import * as sinon from 'sinon';
 
Injectable('Dao', sinon.stub());
const service: Service = new Service();// will get stub dependency
service.create({ entityId: 1 });// will call the stub

Name of dependency's class is the key used to register the dependency instance.

Installation :

npm install --save injects

Package Sidebar

Install

npm i injects

Weekly Downloads

11

Version

1.0.0

License

none

Last publish

Collaborators

  • mickgrz