comet-ioc
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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 !

Readme

Keywords

none

Package Sidebar

Install

npm i comet-ioc

Weekly Downloads

0

Version

2.0.2

License

MIT

Last publish

Collaborators

  • florentindubois