如何使用inversifyJS将值传递给类构造函数?

时间:2017-08-18 00:48:50

标签: inversifyjs

您有一个带有inversify和requirejs的打字稿测试项目,可以克隆:https://github.com/ahedreville/ioctest.git

他的package.json是一个跟随

{
  "name": "ioctest",
  "version": "1.0.0",
  "description": "Test of an IoC with Inversify and RequireJS",
  "scripts": {
    "start": "http-server"
  },
  "author": "Alexandre Hedreville",
  "license": "ISC",
  "devDependencies": {
    "http-server": "^0.10.0"
  },
  "dependencies": {
    "@types/requirejs": "^2.1.30",
    "inversify": "^4.3.0",
    "reflect-metadata": "^0.1.10",
    "requirejs": "^2.3.4"
  }
}

您可以在浏览器上运行此项目,发出以下命令...

mkdir temp
cd temp

git clone https://github.com/ahedreville/ioctest.git
cd ioctest

c:\temp\ioctest>tree /F /A

...
C:.
|   app.main.js
|   index.html
|   package.json
|   README.md
|   tsconfig.json
|
\---src
        App.js
        App.js.map
        App.ts
        ioc_config.js
        ioc_config.js.map
        ioc_config.ts
        Question.js
        Question.js.map
        Question.ts
        Question2.js
        Question2.js.map
        Question2.ts
        Types.js
        Types.js.map
        Types.ts
		
npm install

npm start

http://localhost:8080

正如您所看到的,打开浏览器开发人员控制台日志,接口IProblem将被解析为Question2类,正如我们要求在类ioc_config.ts上执行的那样

问题2课程如下

import { injectable } from "inversify";
import { IProblem } from './Types';

@injectable()
export class Question2 implements IProblem {
    public readonly factor: number = 0;
    public count: number = 0;
    public answer: number = 0;
}

但如果我在ioc_config.ts上更改目标分辨率类

按如下方式更改ioc_config.ts类

import { Container } from 'inversify';
import { sTypes, IProblem } from './Types';
import { Question } from './Question';
import { Question2 } from './Question2';

export const container = new Container();

//container.bind<IProblem>(sTypes.IProblem).to(Question2);
container.bind<IProblem>(sTypes.IProblem).to(Question);

现在,自Question.ts以来,IProblem接口的解析将失败 有一个指导某些价值观的建构者被传入。

问题类如下

import { injectable } from "inversify";
import { IProblem } from './Types';

@injectable()
export class Question implements IProblem {
    public answer: number = 0;
    constructor(public readonly factor: number, public readonly count: number) {
    }
}

我如何解决这个问题(我是inversify的新手)并在解决时将一些值传递给构造函数?

提前致谢

1 个答案:

答案 0 :(得分:0)

我的猜测是你需要一家工厂:

interface Problem {
    factor: number;
    count: number;
}

interface ProblemFactory extends Function {
    (factor: number, count: number): Problem;
}

class Question implements Problem {
    public answer = 0;
    constructor(public readonly factor: number, public readonly count: number) {
    }
}

interface QuestionConstructor {
    new(factor: number, count: number): Question;
}

container.bind<QuestionConstructor>("QuestionConstructor").toConstructor(Question);

container.bind<ProblemFactory>("ProblemFactory").toFactory<Problem>((context) => {
    return (factor: number, count: number) => {
        const Constructor =  context.container.get<QuestionConstructor>("QuestionConstructor");
        return new Constructor(factor, count);
    };
});