inRequestScope 未按预期工作

The inRequestScope is not working as expected

帮助使用inRequestScope inversifyJS 例如:

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

我希望在第二次调用get时返回同一个实例,而不是再次实例化它

您需要将 .get 调用包装在一个 class 和 中,然后 使用 .get 或 class 解析 class 或.resolve

文档摘录

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