使用 NInject 查找类,但使用自己的参数构造该类
我知道这不是一个好的做法。
这里有一些代码演示了这个问题(但实际上不起作用):
public interface IBar {}
public interface Bar : IBar {}
public interface IFoo {}
public class Foo : IFoo
{
public Foo(IBar bar)
{
}
}
public class InjectionModule : NinjectModule
{
public override void Load()
{
Bind<IFoo>().To<Foo>();
}
}
public class MyApp
{
public void DoSomething()
{
// Get a foo with a particular bar
var foo1 = Kernel.Get<IFoo>(new Bar());
// Get another foo with a different bar
var foo2 = Kernel.Get<IFoo>(new Bar());
}
}
所以我想做的是使用 NInject 将 IFoo 绑定到 Foo,但让我的应用程序在运行时向构造函数提供 Bar 参数,而不是 NInject 解决 IBar 依赖关系的通常做法。
I know this is not good practice.
Here is some code that sort of demonstrates the problem (but doesn't actually work):
public interface IBar {}
public interface Bar : IBar {}
public interface IFoo {}
public class Foo : IFoo
{
public Foo(IBar bar)
{
}
}
public class InjectionModule : NinjectModule
{
public override void Load()
{
Bind<IFoo>().To<Foo>();
}
}
public class MyApp
{
public void DoSomething()
{
// Get a foo with a particular bar
var foo1 = Kernel.Get<IFoo>(new Bar());
// Get another foo with a different bar
var foo2 = Kernel.Get<IFoo>(new Bar());
}
}
So what I am trying to do is to use NInject to bind IFoo to Foo, but have my app supply the Bar argument to the constructor at runtime, rather than the usual practice where NInject resolves the IBar dependency.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)