ninject继承接口(interface)

标签 ninject

我有两个基本接口(interface),如下所示,一个继承另一个。我将它们绑定(bind)到实现,如下所示..

public interface IUnitOfWork { }
public interface IEventStoreUnitOfWork : IUnitOfWork { }

public class TransactionalHandler<TCommand, TCommandHandler> {
    public TransactionalHandler(IUnitOfWork unitOfWork) {
            // removed
    }
}

public class EventStoreUnitOfWork : IEventStoreUnitOfWork { }

绑定(bind)...

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)。我尝试了这个,但遇到了同样的错误...

Bind<TransactionalHandler<IDomainCommand, IDomainCommandHandler<IDomainCommand>>>()
 .ToSelf()
 .WithConstructorArgument("unitOfWork", Kernel.GetService(typeof(IEventStoreUnitOfWork)) as IUnitOfWork);

有什么想法吗?

谢谢

最佳答案

Aaron jensen 有一个 ninject fork,如果你绝对需要这种行为,它会考虑 github 上的基本类型

关于ninject继承接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5653178/

相关文章:

asp.net-mvc - 在 ASP.NET MVC3 中使用 Ninject.Wcf。从 NinjectWcfApplication 派生禁用应用程序

c# - 如何解决 Ninject 模块内的依赖关系?

ninject - UnitOfWork 与 NInject 设计问题

asp.net-mvc-3 - 使用 PetaPoco 实现存储库模式

c# - 在 Ninject 中停用时跳过 Dispose

WCF 与 Ninject 抛出 ArgumentNullException

ninject - 使用 NInject 绑定(bind)泛型接口(interface),如果没有设置泛型类型的绑定(bind),则使用默认值

c# - Ninject:通过属性进行拦截,而不从 InterceptAttribute 派生

ninject - ASP.NET Core MVC 中是否继续支持 Ninject?

dependency-injection - 使用 Ninject 并绑定(bind)默认实现,同时避免可怕的 Service Locator 反模式