/ts-di

TypeScript version of Angular Dependency Injection Framework

Primary LanguageTypeScriptOtherNOASSERTION

Build Status

Dependency Injection

This fork - it's just version TypeScript Angular di.js.

Install

npm install ts-di --save

Examples resolve dependency

Inject instance of class A for constructor of class B:

Using decorator

import {Inject} from 'ts-di';

class A{}

// All dependencies in @Inject listed separated by commas
@Inject(A)
class B
{
  constructor(private a: A){}

  getValue()
  {
    console.log(`There should be a instance of class A:`, this.a);
  }
}

Using annotation a function

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) );

Get instance

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);

Usage decorators

Now supports five decorators:

  • For resolving chain dependencies:
    • @Inject
    • @InjectPromise
    • @InjectLazy
  • For resolving single dependency (useful for testing):
    • @Provide
    • @ProvidePromise