Closed robertnisipeanu closed 4 years ago
When you call the constructor directly, you circumvent the IOC framework. Take a look at the Readme section which explains how to prevent direct constructor calls for Singletons.
I think the better code for your use case would be to inject the Singleton dependency:
@Singleton
class Test{
@Inject private test: Test1;
constructor() {
// you can now access this.test
}
}
@Singleton
@OnlyInstantiableByContainer // This makes new Test1() throw a Type Error
class Test1 {
a: number;
constructor(a?: number) {
if(a) this.a = a;
console.log("Test1");
}
}
This way the IOC framework automatically handles your dependency and will provide you always the same instance of the Test1 class.
So I tried following the getting started from NPM however, i stumbled across this problem: when using
new ClassName()
when that class has the@Singleton
decorator, it still calls the constructor of that class every time I callnew ClassName()
, it'll only call once if I useContainer.get(ClassName)
.Example:
Will print the following:
While this:
Would print this:
Why is that the case with the first example?