Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The inRequestScope is not working as expected

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

like image 265
Iuri Brindeiro Avatar asked Jan 18 '26 21:01

Iuri Brindeiro


1 Answers

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

like image 174
episage Avatar answered Jan 21 '26 10:01

episage