InversifyJS binding decorators and lazy inject

3.6k Views Asked by At

I use InversifyJS with binding and lazy inject decorators and I'm getting the next error:

No matching bindings found for serviceIdentifier: Symbol(ClassB)

Here's the code:

inversify.config.ts

import { Container } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import { buildProviderModule, provide } from 'inversify-binding-decorators';
import Types from './types';

const container = new Container();

container.bind<MyType>(Types.MyType)
    .to(MyType);

const { lazyInject } = getDecorators(container, false);

container.load(buildProviderModule());

export { container, lazyInject, provide };

MyType.ts

import { inject, injectable } from 'inversify';

@injectable()
export class MyType{
    constructor(
        @inject(Q) private readonly Q: Q,
        @inject(W) private readonly W: W,
        @inject(E) private readonly E: E) {
    }
}

Main.ts

import 'reflect-metadata';

import './class-a';
import './class-b';

import { MyType } from './my-type';
import Types from './di/types';
import { container } from './di/inversify.config';


const main = async () => {
    const mytype = container.get<MyType>(Types.MyType);
    await mytype.execute();
};

main()
    .then(() => {
        console.info('Started application...');
    })
    .catch((error) => {
        console.error(error);
    });

ClassA.ts

import { inject } from 'inversify';
import Types from '../../di/types';
import { provide } from '../../di/inversify.config';
import { ClassB } from '../../some/directory/class-b';

@provide(Types.ClassA)
export class ClassA implements IClassA {
    constructor(
        @inject(ClassX) private readonly ClassX: ClassX,
        @inject(ClassY) private readonly ClassY: ClassY) {
    }

    @lazyInject(ClassB)
    private readonly ClassB!: ClassB;
}
export default ClassA;

ClassB.ts

import Types from '../../di/types';
import { provide } from '../../di/inversify.config';

@provide(Types.ClassB)
export class ClassB implements IClassB{
...
}
export default ClassB;

types.ts

const Types = {
    ClassA: Symbol.for('ClassA'),
    ClassB: Symbol.for('ClassB'),
};

export default Types;

I tried to follow the answer to this question Binding classes with property injection to kernel in inversify But still no success

0

There are 0 best solutions below