This project has to main goal to provide utility tools to inversify and a dynamic registration of dependencies.
This project is based on inversify.
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 !
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 {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 !