8

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)
)
Estus Flask
  • 206,104
  • 70
  • 425
  • 565

1 Answers1

1

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.

Marc
  • 4,715
  • 3
  • 27
  • 34