Options
All
  • Public
  • Public/Protected
  • All
Menu

Newdash Inject Container

NewDash - Dependency Inject Container

npm (scoped) Codecov Netlify Quality Gate Status Security Rating

Yet another dependency injection container for typescript

Quick Start

# install libs
npm i -S reflect-metadata @newdash/inject
// tsconfig.json
// remember enable decorator related flags
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}
// import reflect lib firstly
import "reflect-metadata"; 
import { inject } from "@newdash/inject"

// a really simple example
it('should support deep constructor injection', async () => {

  class A {
    v: number
    constructor(@inject("v") v) {
      this.v = v;
    }
  }

  class B {
    a: InjectWrappedInstance<A>
    constructor(@inject(A) a) {
      this.a = a;
    }
  }

  const ic = InjectContainer.New();
  ic.registerInstance("v", 999); // define an instance provider in simple way
  const b = await ic.getInstance(B);
  expect(b.a.v).toBe(999);

});

CHANGELOG

LICENSE

Generated using TypeDoc