gpt4 book ai didi

javascript - inRequestScope 未按预期工作

转载 作者:搜寻专家 更新时间:2023-10-30 21:11:55 25 4
gpt4 key购买 nike

帮助使用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 调用包装在一个类中,然后使用 .get.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

关于javascript - inRequestScope 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com