Inversifyjs: Can dependencies be resolved without creating an instance ?

Created on 25 Apr 2017  路  3Comments  路  Source: inversify/InversifyJS

Hi,
I have the following problem:

Some code that I do not control goes as such:
```typescript
function makeSomethingAndRun(something: typeof Test) {
let mything = new something(new Foo());
mything.run();
}
````

So I need to pass it the constructor function / typescript type so that it can create it itself using new...

Now I want to pass a class created via inversify to this:

```typescript
interface ITest {
aTest:ISomething;
}

@injectable()
class Test implements ITest {
constructor(@inject(ISomething) public aTest: ISomething) {
}
}

mainContainer.bind(ITest).to(Test);
``` However as far as I know I can't pass directly the Test constructor to makeSomethingAndRunlikemakeSomethingAndRun(Test)` since the dependencies have not been resolved yet.

And calling mainConainter.get<ITest> will return an instance...

Is it possible to make this work ?

Most helpful comment

Yes it works! thanks!

For reference here's what the test now looks like:

const ISomething = Symbol("ISomething");
const ITest = Symbol("ITest");

interface ISomething {

}
class Foo implements ISomething {
}
interface ITest {
    aTest: ISomething;
    run(): void;
}

@injectable()
class Test implements ITest {
    constructor( @inject(ISomething) public aTest: ISomething) {
    }

    run() {
        console.log("test");
    }

}

function makeSomethingAndRun(something: typeof Test) {
    let mything = new something(new Foo());
    mything.run();
}

mainContainer.bind<interfaces.Newable<ITest>>("Newable<ITest>").toConstructor<ITest>(Test);

let TestConstructor = mainContainer.get<interfaces.Newable<ITest>>("Newable<ITest>");

makeSomethingAndRun(TestConstructor);

All 3 comments

I think this is what you are looking for https://github.com/inversify/InversifyJS/blob/master/wiki/constructor_injection.md

mainContainer.bind<interfaces.Newable<ITest>>("Newable<ITest>").toConstructor<ITest>(Test);
// should return the constructor of Test that can be passed down to the "makeSomethingAndRun()"

@Dirrk thanks for helping here :+1: @hexa00 can you please confirm that this solves your issue and close the issue if so? Thanks!

Yes it works! thanks!

For reference here's what the test now looks like:

const ISomething = Symbol("ISomething");
const ITest = Symbol("ITest");

interface ISomething {

}
class Foo implements ISomething {
}
interface ITest {
    aTest: ISomething;
    run(): void;
}

@injectable()
class Test implements ITest {
    constructor( @inject(ISomething) public aTest: ISomething) {
    }

    run() {
        console.log("test");
    }

}

function makeSomethingAndRun(something: typeof Test) {
    let mything = new something(new Foo());
    mything.run();
}

mainContainer.bind<interfaces.Newable<ITest>>("Newable<ITest>").toConstructor<ITest>(Test);

let TestConstructor = mainContainer.get<interfaces.Newable<ITest>>("Newable<ITest>");

makeSomethingAndRun(TestConstructor);
Was this page helpful?
0 / 5 - 0 ratings