多个 ninject 绑定是否保证维持其绑定顺序
如果我注册:
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Knife>();
Bind<IWeapon>().To<ChuckNorris>();
然后通过以下方式检索:
IEnumerable<IWeapon> weapons = ServiceLocator.Current.GetAllInstances<IWeapon>();
我是否保证绑定将始终按该顺序交还?
我尝试了一下,看起来确实如此,但这可能纯粹是偶然的。
If I register:
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Knife>();
Bind<IWeapon>().To<ChuckNorris>();
And then retrieve via:
IEnumerable<IWeapon> weapons = ServiceLocator.Current.GetAllInstances<IWeapon>();
Am I guaranteed the bindings will always be handed back in that order?
I tried it, and it seems it does, but this could be purely incidental.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:不,你不是!
更长一点的答案:当前的实现保持了顺序。但不保证 Ninject 的未来版本中仍然会出现这种情况。此外,您不应该在 IoC 容器配置中包含此类业务规则。
Short answer: No, you aren't!
A bit longer answer: The current implementation keeps the order. But it is not garanteed that this will still be the case in future versions of Ninject. Also you shouldn't have such business rules in the IoC container configuration.
我找到了一种进行有序多重绑定的方法,因为我也需要这个:(
诚然与我的答案非常相似:https:// stackoverflow.com/a/56306527/50088)
我同意,仅指定未来版本将保留声明顺序是一个更好的解决方案,但此解决方法有效。
我希望在上下文 c 中创建子对象 c,这样 Get 就会知道它被注入到 InjectedClass 中,但我不知道如何做到这一点。
I found a way to do ordered multi-bindings, because I needed this too:
(admittedly very similar to my answer here: https://stackoverflow.com/a/56306527/50088)
I agree that just specifying that future versions will preserve the declaration order is a better solution, but this workaround works.
I would have liked to create the child object c within the context c, so Get would know it was being injected into InjectedClass, but I could not figure out how to do that.