Ninject 2 中的上下文变量
我在早期版本中找到了关于上下文变量的这篇文章忍者。我的问题有两个方面。首先,如何使用 Ninject 2 获得这种行为?其次,上下文变量是否沿着请求链传递?例如,假设我想将这些调用替换
var a = new A(new B(new C())));
var specialA = new A(new B(new SpecialC()));
为:...
var a = kernel.Get<A>();
var specialA = kernel.Get<A>(With.Parameters.ContextVariable("special", "true"));
是否可以设置这样的绑定,其中上下文会记住在构造 < 时它处于“特殊”上下文中代码>C?
I found this article on Context Variables in an earlier version of Ninject. My question is two-fold. First, how can I get this behavior with Ninject 2? Secondly, do context variables carry through down the request chain? For example, let's say I wanted to replace these calls:
var a = new A(new B(new C())));
var specialA = new A(new B(new SpecialC()));
... with this:
var a = kernel.Get<A>();
var specialA = kernel.Get<A>(With.Parameters.ContextVariable("special", "true"));
Is it possible to set up a binding like this, where the context remembers that it is in a "special" context when it comes time to construct a C
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我在 V2 上使用的一些东西,花了约 0 的努力来为你清理它 - 如果你无法解决它,请告诉我。
正如您所猜测的,似乎没有一个真正明确的 API 可以按原样显示 v2 中的“上下文参数,即使是嵌套解析”(它的存在被隐藏为
重载中的第三个参数)参数
ctor)。这里有一些代码[这会让事情变得混乱],它演示了我如何在我的上下文中应用它:-
像往常一样,你应该去看源代码和测试-它们将为你提供更详细和相关的答案比我能。
Here's some stuff that I use against V2, with ~0 effort to clean it up for you - let me know if you can't disentagle it.
As you surmised, there doesn't seem to be a really explicit API that surfaces the "context parameter, even for nested resolutions" stuff in v2 as-is (it's presence is buried as the 3rd parameter on an overload of the
Parameter
ctor).And here's some code [that'll confuse the matter] which demonstrates how I apply it in my context:-
As usual, you should go look a the source and the tests - they'll provide you with a far more detailed and relevant answer than I can.