Help using inRequestScope inversifyJS Eg:
container.bind<ITransactionManager>(Types.MysqlTransactionManager).to(MysqlTransactionManager).inRequestScope()
...
container.get<ITransactionManager>(Types.MysqlTransactionManager)//call the MysqlTransactionManager constructor and return the instance
container.get<ITransactionManager>(Types.MysqlTransactionManager) //call the constructor one more time and return a new instance
I want the same instance to be returned when the get is called for the second time instead of instantiating it again
You need to wrap both .get
calls in a single class and then resolve the class using .get
or .resolve
Excerpt from documentation
Each call to one of this methods will resolve a root dependency and all its sub-dependencies. Internally, a dependency graph known as the "resolution plan" is created by InversifyJS. The inRequestScope scope will use one single instance for objects that appear multiple times in the resolution plan. This reduces the number of required resolutions and it can be used as a performance optimization in some cases.
https://github.com/inversify/InversifyJS/blob/master/wiki/scope.md