[英]How can I Bind by convention a generic interface to a multiple generic concrete type in Ninject
I have a multiple object that implements the same generic interface. 我有一个实现相同通用接口的多个对象。 So, I want to configure it using Ninject conventions but I don't know how to do it.
因此,我想使用Ninject约定对其进行配置,但是我不知道该怎么做。
Right now I have these registrations 现在我有这些注册
Bind<IQueryHandler<GetPagedPostsQuery, PagedResult<Post>>>().To<GetPagedPostsQueryHandler>();
Bind<IQueryHandler<GetPostByDateQuery, Post>>().To<GetPostByDateQueryHandler>();
I tried this convention 我尝试过这个约定
Kernel.Bind(x => x
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom(typeof(IQueryHandler<,>))
.BindDefaultInterface());
But doesn't register any query handler. 但不注册任何查询处理程序。
Is posible to do it with conventions? 可以用惯例做到这一点吗?
** Edit ** The classes are the followings **编辑**这些类别如下
public class GetPagedPostsQueryHandler : IQueryHandler<GetPagedPostsQuery, PagedResult<Post>>
public class GetPostByDateQueryHandler : IQueryHandler<GetPostByDateQuery, Post>
BindDefaultInterface
means that MyService : IMyService, IWhatever
will be bound to IMyService
. BindDefaultInterface
意味着MyService : IMyService, IWhatever
将绑定到IMyService
。
You should use BindSingleInterface
, which worked instantly when I tried it in a Unit Test: 您应该使用
BindSingleInterface
,当我在单元测试中尝试它时,它可以立即工作:
[TestMethod]
public void TestMethod2()
{
var kernel = new StandardKernel();
kernel.Bind(c => c.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom(typeof(IQueryHandler<,>))
.BindSingleInterface());
kernel.TryGet<IQueryHandler<GetPagedPostsQuery,PagedResult<Post>>>()
.Should()
.NotBeNull();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.