Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inversify toFactory vs toDynamicValue

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)
)
like image 855
Estus Flask Avatar asked Oct 10 '18 18:10

Estus Flask


1 Answers

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.

like image 66
Marc Avatar answered Oct 04 '22 19:10

Marc