[英]Ninject Bind AllClasses when Concrete implements Abstract
I have (a very contrived example): 我有一个非常人为的例子:
interface IStrategy
{
bool CanHandle(SomeEnum someEnum);
void Handle(SomeEntity someEntity);
void ConfigureDb(DbContext context);
}
abstract BaseStrategy : IStrategy
{
abstract bool CanHandle(SomeEnum someEnum);
abstract void Handle(SomeEntity someEntity);
virtual void Configure(DbContext context)
{
context.LazyLoading = true;
}
}
class SomeEntityStrategy : BaseStrategy
{
/* Assume this has been implemented */
}
class SomeOtherEntityStrategy : BaseStrategy
{
/* Assume this has been implemented */
}
So I assumed that: 所以我认为:
kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IStrategy)).BindDefaultInterface());
Would bind all the concrete implementations to the interface IStrategy
. 将所有具体实现绑定到IStrategy
接口。
In my constructor I do: 在我的构造函数中,我这样做:
ctor(IEnumerable<IStrategy> strategies)
This comes back with an empty list. 这将返回一个空列表。
So this may have been answered some where else, but I'm not entirely sure what I should be searching on. 因此,这可能在其他地方得到了回答,但是我不确定我应该搜索什么。
So for some reason, that I wouldn't mind having clarified why does BindAllInterfaces
work??? 所以出于某种原因,我不介意澄清为什么BindAllInterfaces
工作???
So my thinking is that Ninject treats abstract classes as interfaces. 所以我的想法是Ninject将抽象类视为接口。
So I have changed the binding slightly: 因此,我对绑定进行了一些更改:
kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IStrategy)).BindDefaultInterfaces());
So just the pluralisation is the issue.. 因此,只有多元化才是问题。
My thinking is that Ninject is treating the abstract class an interface so technically there are 2 interfaces that the strategy can be bound to. 我的想法是Ninject将抽象类视为一个接口,因此从技术上讲,该策略可以绑定到2个接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.