我想用Ninject绑定装饰类。
public interface ICommandBus{ Send<T>(T command);}
// This is my first CommandBus class I will decorate this.
public class CommandBus: ICommandBus {
public void send(){ //handle commands... }
}
我创建了一个CommandBusDecorator
public abstract class CommandBusDecorator: ICommandBus
{
private readonly ICommandBus commandBus;
protected CommandBusDecorator(ICommandBus commandBus)
{
this.commandBus = commandBus;
}
public virtual void Send<T>(T command)
{
commandBus.SendCommand(command);
}
}
一个装饰的类来应用。
public class DecoratedFirstCommandBus: CommandBusDecorator{ .... }
我想用Ninject绑定依赖项,但是Ninject不包含装饰器实现。
Kernel.Bind<ICommandBus>().To<CommandBusDecorator>();
Kernel.Bind<ICommandBus>().To<CommandBus>();
Kernel.Bind<ICommandBus>().To<CommandBus>()
.WhenInjectedInto<DecoratedFirstCommandBus>();
但是发生了错误:
&#34; ExceptionMessage&#34;:&#34;尝试创建类型为&#39; ValuesController&#39;的控制器时发生错误。确保控制器具有无参数的公共构造函数。&#34;
值控制器只接受一个构造函数,
public class ValuesController : ApiController
{
private ICommandBus commandBus;
public ValuesController(ICommandBus commandBus)
{
this.commandBus = commandBus;
}
}
如果我只使用一个绑定:Kernel.Bind<ICommandBus>().To<CommandBus>();
申请正在运作。