如何解决ninject继承的接口
| 我有以下两个基本接口,一个继承另一个。我将它们绑定到以下实现。public interface IUnitOfWork { }
public interface IEventStoreUnitOfWork : IUnitOfWork { }
public class TransactionalHandler<TCommand,TCommandHandler> {
public TransactionalHandler(IUnitOfWork unitOfWork) {
// removed
}
}
public class EventStoreUnitOfWork : IEventStoreUnitOfWork { }
绑定...
Bind<IEventStoreUnitOfWork>().To<EventStoreUnitOfWork>();
Bind<TransactionalHandler<IDomainCommand,IDomainCommandHandler<IDomainCommand>>>()
.ToSelf()
.WithConstructorArgument(\"unitOfWork\",Kernel.GetService(typeof(IEventStoreUnitOfWork)));
这是行不通的。错误是:
Error activating IUnitOfWork
No matching bindings are available,and the type is not self-bindable.
Activation path:
2) Injection of dependency IUnitOfWork into parameter unitOfWork of constructor of type TransactionalHandler{CreateUserCmd,CreateUserCmdHandler}
1) Request for TransactionalHandler{CreateUserCmd,CreateUserCmdHandler}
这里发生了什么?显然,IUnitOfWork是由IEventStoreUnitOfWork继承的,并且具有绑定。我事件尝试了这个,但是我得到了同样的错误...
Bind<TransactionalHandler<IDomainCommand,IDomainCommandHandler<IDomainCommand>>>()
.ToSelf()
.WithConstructorArgument(\"unitOfWork\",Kernel.GetService(typeof(IEventStoreUnitOfWork)) as IUnitOfWork);
有任何想法吗?
谢谢
解决方法
类型必须完全匹配。这意味着您需要为IUnitOfWork绑定。搜索依赖项时,不考虑基本类型。
,Aaron jensen有一个ninject分支,如果您绝对需要这种行为,它将考虑github上的基本类型
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。