Open MeirionHughes opened 6 years ago
I have tried to reproduce the same case (A injected in B. B injected in A.) with @lazyInject but also getting an error. And also I tried an example with the service identifier. https://github.com/inversify/InversifyJS/issues/865
Any progress on this issue?
Any updates ? facing same issue with lazyServiceIdentifier
I resolved this problem by defining a third class, class C, which has an A and B injected property. This will contain methods which should call each property's methods.
This really really hurts.
Just had an issue with that. My workaround:
a) Inject Container
to container somewhere
import { Container } from 'inversify'
const container = new container({...})
container.bind(Container).toConstantValue(container)
b) Implement custom lazy inject getter decorator based on container.get()
import { interfaces, decorate, Container, METADATA_KEY } from 'inversify'
export function lazyInject(
serviceIdentifierCallback: () => interfaces.ServiceIdentifier<any>,
): PropertyDecorator {
return function (target: any, propertyKey) {
// inject container as this.container if it's not present
if (!Reflect.getMetadata(METADATA_KEY.TAGGED_PROP, target.constructor)?.container) {
decorate(inject(Container), target, 'container')
}
// define getter
Object.defineProperty(target, propertyKey, {
get: function () {
return this.container.get(serviceIdentifierCallback())
},
})
}
}
c) Usage
@injectable()
export class MyClass {
@lazyInject(() => SOME_TOKEN) service!: SomeService
}
The caveats are:
Any progress on this issue?
I don't think anyone is working on Inversify
Unable to lazily inject circular service dependency via property injection.
Side-note: Identifer -> Identifier
Expected Behavior
Inject without error
Current Behavior
throws error
Steps to Reproduce (for bugs)
Context
I'm aware that injecting via the constructor works; however in NInject I was able to do this kind of lazy property injection within a base "Module" class. Subsequent derivations of that "Module" class then didn't have to deal with adding those dependencies manually.
Environment
inversify: "^4.13.0" node-js: 10.1.0
Stack trace