This related question shows examples from the documentation but doesn't explain practical difference between InversifyJS toFactory
and toDynamicValue
toDynamicValue
accepts factory function while toFactory
accepts higher-order factory function. But toDynamicValue
could return factory function as a value, too.
Can they serve same purpose and how can their behaviour be different?
How do Bar
and Baz
differ in this example?
container.bind('Bar').toDynamicValue(
context => fooName => context.container.getNamed(Foo, fooName)
)
container.bind('Baz').toFactory(
context => fooName => context.container.getNamed(Foo, fooName)
)
As I see it the dynamic value syntax is the one you pass into any value / lambda that results in creating an instance.
The Factory syntax results in a factory where you also get the context. From the context you can get the container and use service-location to resolve other objects you need construct your object instance. So its a indirection supplying the context and therefore the container too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With