InversifyJS - What is the difference between .to and .toService?

170 views Asked by At

I'm confused by the documentation, which states that .toService(MyClass) is used for transitive bindings. But in the examples shown, i can achieve the same thing using a regular .to(MyClass) as well.

https://github.com/inversify/InversifyJS/blob/master/wiki/transitive_bindings.md

What is the concrete use case of .toService()?

1

There are 1 answers

2
samivic On

You use the .to() method is used when you are binding an interface or symbol to a concrete class.

container.bind(B).to(A);

For the .toService() method is used when you want to bind an interface or symbol to an already bound service in the container. InversifyJS will not create a new instance, but will return the same instance that was bound to the original service.

container.bind(C).toService(B);