gpt4 book ai didi

ninject 继承接口(interface)

转载 作者:行者123 更新时间:2023-12-04 06:29:10 24 4
gpt4 key购买 nike

我有如下两个基本接口(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com