2017-10-16 71 views
1

我有这3个接口:绑定到一个类多个接口

interface IA {} 
interface IB {} 
interface IC {} 

另外,我这等接口,从IAIBIC继承:

interface II : IA, IB, IC {} 

然后,我也创建了一个类CC继承自II

class CC : II {} 

我创建这些绑定:

this.Bind<IA>().To<CC>().InSingletonScope(); 
this.Bind<IB>().To<CC>().InSingletonScope(); 
this.Bind<IC>().To<CC>().InSingletonScope(); 
this.Bind<II>().To<CC>().InSingletonScope(); 

我不知道,每次我已经请求了哪个接口时间,NInject内核是要给的CC同一单一实例。

所以,我的意思是:

IA ia = kernel.Get<IA>(); 
IB ib = kernel.Get<IB>(); 

ia是相同的实例ib

我怎么能得到这种行为?

+0

[在Ninject中绑定单个服务到多个服务]可能的重复(https://stackoverflow.com/questions/3147996/binding-singleton-to-multiple-services-in-ninject) – BatteryBackupUnit

+0

区别于[你的问题来自几乎整整一年前](https://stackoverflow.com/questions/40125455/ninject-bind-multiple-types-to-the-same-singleton-instance)是分钟。 – BatteryBackupUnit

回答

2

据我所知,这应该工作:

this.Bind<IA, IB, IC, II>().To<CC>().InSingletonScope(); 

Bind超载需要多达四个类型参数。

+2

如果四个不够,总会有'Bind(params Type []服务)''。 –