Options
All
  • Public
  • Public/Protected
  • All
Menu

styled with prettier Greenkeeper badge Travis Coveralls Dev Dependencies

Library for DI with global container underhood

This library was inpired by AngularJS

  npm i inject-dependency

Typescript Example:

    import {Context, singleton, injectable, dependencies, lazyInject, fabrica} from 'inject-dependency'

    @singleton
    @injectable()
    class A {
      @lazyInject<C>('C', a => a.c.hello())
      c: C;

      hello() {
        console.log('A');
      }
    }

    @singleton
    @dependencies(A)
    @injectable()
    class B {
      constructor(public a: A) {}

      hello() {
        console.log('B');
      }
    }
    @singleton
    @dependencies(B)
    @injectable('C')
    class C {
      constructor(public b: B) {}

      hello() {
        console.log('C');
      }
    }

    const cInstance = Context.resolve<C>(C);

    cInstance.b.a.hello();
    cInstance.b.hello();
    cInstance.hello();

    /*
      A
      B
      C
      C
    */

ES7 Example:

    import {Context, singleton, injectable, dependencies, lazyInject, fabrica} from 'inject-dependency'

    @singleton
    @injectable()
    class A {
      @lazyInject('C', a => a.c.hello()) 
      c;

      hello() {
        console.log('A');
      }
    }

    @singleton
    @dependencies(A)
    @injectable()
    class B {
      constructor(a) {
        this.a = a
      }

      hello() {
        console.log('B');
      }
    }
    @singleton
    @dependencies(B)
    @injectable('C')
    class C {
      constructor(b) {
        this.b = b
      }

      hello() {
        console.log('C');
      }
    }

    const cInstance = Context.resolve(C);

    cInstance.b.a.hello();
    cInstance.b.hello();
    cInstance.hello();

    /*
      A
      B
      C
      C
    */

TODO:

  • [ ] Add more unit tests
  • [ ] Improve code documentation

Index

Functions

Private classToFunction

  • classToFunction<F>(cls: F): function

dependencies

  • dependencies(...clsOrAliases: any[]): classDecorator

fabrica

  • fabrica<F>(fabrica: function): classDecorator
  • class decorator

    Type parameters

    • F: Function

    Parameters

    • fabrica: function
        • (): any
        • Returns any

    Returns classDecorator

injectable

  • injectable(alias?: any): classDecorator

Private invariant

  • invariant(state: boolean, errorMessage: string): void

lazyInject

  • lazyInject<C>(clsOrAlias: any, callback?: function): propertyDecorator
  • property decorator

    Type parameters

    • C: Function

    Parameters

    • clsOrAlias: any
    • Default value callback: function = noop
        • (thus: C): void
        • Parameters

          • thus: C

          Returns void

    Returns propertyDecorator

Private noop

  • noop(): void

Private once

  • once<F>(f: F): F

singleton

  • singleton(ctr: Function): void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc