di-typescript

  • import {Inject} from ‘ts-di’; class A{} // All dependencies in @Inject listed separated by commas @Inject class B { constructor(private a: A){} getValue() { console.log(`There should be a instance of class A:`, this.a); } }
  • /path/to/class/B’; let injector = new Injector(); // Instance class B and resolve dependency let instance = injector.get(B);
  • Another benefit from reflect-metadata is, that Inject don’t need parameters anymore – It now retrieves the dependencies through the `design:paramtypes meta information.
  • import {annotate, InjectDecorator} from ‘ts-di’; class A{} class B { constructor(private a: A){} getValue() { console.log(`There should be a instance of class A:`, this.a); } } // All dependencies in InjectDecorator listed separated by commas annotate( B, new InjectDecorator(A) );
  • The version uses reflect-metadata to store the meta information provided by the decorators.

A DI framework, fork from angular/di.js

@npmdaily: di-typescript – A DI framework, fork from angular/di.js #npm #javascript #nodejs

import {Inject} from ‘ts-di’; class A{} // All dependencies in @Inject listed separated by commas @Inject class B { constructor(private a: A){} getValue() { console.log(`There should be a instance of class A:`, this.a); } }

import {annotate, InjectDecorator} from ‘ts-di’; class A{} class B { constructor(private a: A){} getValue() { console.log(`There should be a instance of class A:`, this.a); } } // All dependencies in InjectDecorator listed separated by commas annotate( B, new InjectDecorator(A) );

import {Injector} from ‘ts-di’; import {B} from ‘./path/to/class/B’; let injector = new Injector(); // Instance class B and resolve dependency let instance = injector.get(B);

di-typescript