2.0.2 • Published 7 years ago

comet-ioc v2.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

comet-ioc

Codacy Badge NSP Status Build Status

Roles

This project has to main goal to provide utility tools to inversify and a dynamic registration of dependencies.

Usage

This project is based on inversify.

Simple usage

To understand how works this project, below there is an example.

import {inject, injectable, bootstrap} from 'comet-ioc'

@injectable()
export class A {
  hi() {
    console.log('hi !')
  }
}

@injectable()
export class B {
  constructor(@inject(A) a: A) {
    a.hi()
  }
}

bootstrap(B, {
  declarations: [
    A
  ]
})

result:

hi !

Advance usage

You can also use constant or factory to provide classes, below an example:

import {inject, injectable, bootstrap, interfaces} from 'comet-ioc'

const hi: symbol = Symbol('hi')
const log: symbol = Symbol('log')

@injectable()
export class A {
  hi() {
    this.log(this.hi)
  }

  @inject(hi)
  private hi: string

  @inject(log)
  private log: Function
}

@injectable()
export class B {
  constructor(@inject(A) a: A) {
    a.hi()
  }
}

bootstrap(B, {
  declarations: [
    A
  ],

  constants: [{
    provide: hi,
    useValue: 'hi !'
  }],

  providers: [{
    provide: log,
    useFactory(context: interfaces.Context) {
      return console.log
    }
  }]
})

result:

hi !

Import / Export usage

import {inject, injectable, bootstrap, IBootstrapDependencies} from 'comet-ioc'

@injectable()
class A {
  hi() {
    console.log('hi !')
  }
}

@injectable()
class B {
  constructor(@inject(A) a: A) {
    a.hi()
  }
}

export const FakeModule: IBootstrapDependencies = {
  declarations: [A, B]
}
import {bootstrap, injectable, inject} from 'comet-ioc'
import {FakeModule} from 'comet-ioc-fake'

@injectable()
class App {
  constructor(@inject(B) a: B) { }
}

bootstrap(App, {
  imports: [FakeModule]
})

result:

hi !