Ninject绑定,接口界面 [英] Ninject Binding, Interface to Interface
本文介绍了Ninject绑定,接口界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的事的这个线的东西:
I want to do something along the lines of this:
kernel.Bind<IBootTaskA>().To<BootTaskA>().InSingletonScope();
kernel.Bind<IBootTaskB>().To<BootTaskB>().InSingletonScope();
kernel.Bind<IBootTask>().To<IBootTaskA>();
kernel.Bind<IBootTask>().To<IBootTaskB>();
所以,我可以做到这一点:
So i can do this:
public class Boot
{
public Boot(IBootTask[] bootTasks)
{
foreach(var task in bootTasks){task.Execute();}
}
}
但我似乎无法将一个接口绑定在接口上,任何人都知道解决这个?
but i cant seem to bind an interface to an interface, anyone know a way around this?
推荐答案
继承人你怎么做。
public class Service : IServiceA, IServiceB {}
this.Bind<Service>().ToSelf().InSingletonScope();
kernel.BindInterfaceToBinding<IServiceA, Service>();
kernel.BindInterfaceToBinding<IServiceB, Service>();
的
ninject延伸处理你所需要的。
The ninject extention handles what you need.
https://github.com/ninject/ninject .extensions.contextpreservation /维基/绑定接口到绑定
编辑:
在ninject 3这是稍微容易些,你不再需要contextpreservation,简单的:
In ninject 3 this is slightly easier, you no longer need contextpreservation, Simply:
Bind<IServiceA,IServiceB>().To<Service>().InSingletonScope();
这篇关于Ninject绑定,接口界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文