What's the recommended way of doing lazy resolving? #1818
-
I noticed that the repo of inject decorators is not maintained anymore, so what am I supposed to do if I hope to defer the resolving of some services (maybe most services)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @GarfieldJiang , we do not recommend using any sort of lazy resolution. The most likely reason to go for it is to bypass a circular dependency, and they are almost always a smell of a bad design choice. Could I know more about your specific use case? There's not an answer that covers every possible workaround to lazy resolution. Basically, there should be a way to define dependencies for your services. If you really cannot find an alternative to lazy resolution, you might bind the container as a service to achieve lazy resolution: @injectable()
class Foo {
readonly #container: Container;
constructor(
@inject(Symbol.for('container')
container: Container,
) {
this.#container = container;
}
get foo() {
return this.#container.get('foo'); // you might want to store this in a property
}
} |
Beta Was this translation helpful? Give feedback.
Hey @GarfieldJiang , we do not recommend using any sort of lazy resolution. The most likely reason to go for it is to bypass a circular dependency, and they are almost always a smell of a bad design choice.
Could I know more about your specific use case? There's not an answer that covers every possible workaround to lazy resolution.
Basically, there should be a way to define dependencies for your services. If you really cannot find an alternative to lazy resolution, you might bind the container as a service to achieve lazy resolution: