项目作者: starry-comet

项目描述 :
Inversion of control used in starry-comet projects
高级语言: TypeScript
项目地址: git://github.com/starry-comet/comet-ioc.git
创建时间: 2017-03-16T15:26:13Z
项目社区:https://github.com/starry-comet/comet-ioc

开源协议:MIT License

下载


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.

  1. import {inject, injectable, bootstrap} from 'comet-ioc'
  2. @injectable()
  3. export class A {
  4. hi() {
  5. console.log('hi !')
  6. }
  7. }
  8. @injectable()
  9. export class B {
  10. constructor(@inject(A) a: A) {
  11. a.hi()
  12. }
  13. }
  14. bootstrap(B, {
  15. declarations: [
  16. A
  17. ]
  18. })

result:

  1. hi !

Advance usage

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

  1. import {inject, injectable, bootstrap, interfaces} from 'comet-ioc'
  2. const hi: symbol = Symbol('hi')
  3. const log: symbol = Symbol('log')
  4. @injectable()
  5. export class A {
  6. hi() {
  7. this.log(this.hi)
  8. }
  9. @inject(hi)
  10. private hi: string
  11. @inject(log)
  12. private log: Function
  13. }
  14. @injectable()
  15. export class B {
  16. constructor(@inject(A) a: A) {
  17. a.hi()
  18. }
  19. }
  20. bootstrap(B, {
  21. declarations: [
  22. A
  23. ],
  24. constants: [{
  25. provide: hi,
  26. useValue: 'hi !'
  27. }],
  28. providers: [{
  29. provide: log,
  30. useFactory(context: interfaces.Context) {
  31. return console.log
  32. }
  33. }]
  34. })

result:

  1. hi !

Import / Export usage

  1. import {inject, injectable, bootstrap, IBootstrapDependencies} from 'comet-ioc'
  2. @injectable()
  3. class A {
  4. hi() {
  5. console.log('hi !')
  6. }
  7. }
  8. @injectable()
  9. class B {
  10. constructor(@inject(A) a: A) {
  11. a.hi()
  12. }
  13. }
  14. export const FakeModule: IBootstrapDependencies = {
  15. declarations: [A, B]
  16. }
  1. import {bootstrap, injectable, inject} from 'comet-ioc'
  2. import {FakeModule} from 'comet-ioc-fake'
  3. @injectable()
  4. class App {
  5. constructor(@inject(B) a: B) { }
  6. }
  7. bootstrap(App, {
  8. imports: [FakeModule]
  9. })

result:

  1. hi !