inRequestScope未按预期工作

4

使用inRequestScope反转控制的帮助

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 方法时,返回相同的实例而不是重新实例化

1个回答

1
你需要将两个.get调用都包装在一个类中,然后使用.get.resolve来解析该类。 文档摘录 每次调用这些方法之一都会解析根依赖项及其所有子依赖项。在InversifyJS内部,会创建一个称为“解析计划”的依赖关系图。inRequestScope范围将为在解析计划中多次出现的对象使用单个实例。这减少了所需的解析次数,并可在某些情况下用作性能优化。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接