Closed nilscox closed 11 months ago
Hello, By default bindFactory()
uses the scope "singleton" for the factory. This means the following.
According to the example, TOKENS.class
is bound to the parent container, so its dependencies are resolved from the parent container.
The "scoped" scope option can be used to override dependencies:
The example can be fixed in two ways:
Adding a "scoped" option to bindFactory()
of the parent container.
container.bindValue(TOKENS.value, 42);
container.bindFactory(TOKENS.class, injectableClass(Test, TOKENS.value), {scope: "scoped"});
Binding a second factory with the "scoped" option to the child container.
childContainer.bindValue(TOKENS.value, 51);
childContainer.bindFactory(TOKENS.class, injectableClass(Test, TOKENS.value), {scope: "scoped"});
I'm trying to override an injected value in a child container, but the value passed to the class constructor is not overridden. Here is a test case to demonstrate what I'm trying to achieve:
Is this the expected behavior? I'm using this pattern in tests: I create a global container instance with production bindings, and I want to create a child instance in my tests to overrides some dependencies for testing purpose.
Thanks for making this library, I really enjoy using it!