0

Hi have a typescript test project with inversify and requirejs that can be cloned from: https://github.com/ahedreville/ioctest.git

His package.json is a follows

{
  "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"
  }
}

You can get this project running on your browser issuing the following commands...

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

As you can see, opening the browser developer console log, the interface IProblem is resolved to Question2 class as we requested to do on class ioc_config.ts

Question2 class is as follows

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;
}

but if I change the target resolution class on ioc_config.ts

changing ioc_config.ts class as follows

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);

now the resolution of the IProblem interface will fail since Question.ts HAS A CONSTRUCTOR THAT EXPECT SOME VALUES to get passed into.

Question class is as follows

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) {
    }
}

How can I solve this issue (I am newbie on inversify) and pass some values at resolution time to the constructor?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
alhpe
  • 1,424
  • 18
  • 24
  • I had found a solution using a factory, but I am havings some side effects on my code style. Mainly 1) the class has to be constructor less what obscures the needs of the class to be properly constructed as SOLID demands and 2) is Too verbose. Can some one help to improve this solution? new code files on https://github.com/ahedreville/ioctest2.git – alhpe Aug 18 '17 at 11:23
  • Finally I adopted aurelia dependency injection plugin that is solving all my needs: https://github.com/aurelia/dependency-injection/blob/master/doc/article/en-US/dependency-injection-basics.md – alhpe Sep 13 '17 at 21:11

1 Answers1

0

My guess is that you need a factory:

    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);
        };
    });
nurgasemetey
  • 752
  • 3
  • 15
  • 39
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93