Ninject 装饰器模式
Decorator pattern with Ninject
我正在尝试应用 this 方法来制作验证包装器。我创建了以下接口,我使用 Ninject 作为我的 DI 容器
public interface ICommandHandler<TCommand>
{
void Handle(TCommand command);
}
public interface IValidator<T>
{
IEnumerable<ValidationResult> Validate(T command);
}
public class ValidationCommandHandlerDecorator<T>:ICommandHandler<T>
{
private readonly ICommandHandler<T> _decoratee;
private readonly IValidator<T> _validator;
public ValidationCommandHandlerDecorator(ICommandHandler<T> decoratee,IValidator<T> validator )
{
_decoratee = decoratee;
_validator = validator;
}
public void Handle(T command)
{
this._validator.Validate(command);
this._decoratee.Handle(command);
}
}
public class SaveNewOsiRequestController : ApiController
{
private readonly ICommandHandler<OsiRequest> _osiRequestSaveHandler;
public SaveNewOsiRequestController(ICommandHandler<OsiRequest> osiRequestSaveHandler)
{
_osiRequestSaveHandler = osiRequestSaveHandler;
}
}
下面是我如何将具体的 类 注入控制器:
kernel.Bind(typeof (IValidator<>)).To(typeof (OsiRequestValidator));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (SaveOsiRequestCommandHandler))
.WhenInjectedInto(typeof (ValidationCommandHandlerDecorator<>));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto(typeof(SaveNewOsiRequestController));
代码运行良好,没有任何问题。然而,问题是如何使用 Ninject 对另一个控制器进行相同的注入?为了简单起见,我想像这样将 Another Validator
和 Another CommandHandler
注入 Another Controller
:
kernel.Bind(typeof (IValidator<>)).To(typeof (ANOTHER_Validator));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ANOTHER_CommandHandler))
.WhenInjectedInto(typeof (ValidationCommandHandlerDecorator<>));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto(typeof(ANOTHER_RequestController));
这里的问题是您正在进行通用绑定,但您是在特定情况下使用它。当您希望泛型绑定适用于该绑定的所有情况(或者至少是您可以有条件地绑定到的所有情况)时,您只想使用泛型绑定。
为此,您可以轻松地做到这一点:
kernel.Bind<IValidator<OsiRequest>>().To<OsiRequestValidator>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<SaveOsiRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<OsiRequest>>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<ValidationCommandHandlerDecorator<OsiRequest>()
.WhenInjectedInto<SaveNewOsiRequestController>();
那么每增加一个就是:
kernel.Bind<IValidator<DifferentRequest>>().To<DifferentValidator>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<DifferentRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<DifferentRequest>>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<ValidationCommandHandlerDecorator<DifferentRequest>()
.WhenInjectedInto<DifferentRequestController>();
您可以更改最后一个绑定,这样您就不必为每种类型声明它:
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto<ApiController>();
我正在尝试应用 this 方法来制作验证包装器。我创建了以下接口,我使用 Ninject 作为我的 DI 容器
public interface ICommandHandler<TCommand>
{
void Handle(TCommand command);
}
public interface IValidator<T>
{
IEnumerable<ValidationResult> Validate(T command);
}
public class ValidationCommandHandlerDecorator<T>:ICommandHandler<T>
{
private readonly ICommandHandler<T> _decoratee;
private readonly IValidator<T> _validator;
public ValidationCommandHandlerDecorator(ICommandHandler<T> decoratee,IValidator<T> validator )
{
_decoratee = decoratee;
_validator = validator;
}
public void Handle(T command)
{
this._validator.Validate(command);
this._decoratee.Handle(command);
}
}
public class SaveNewOsiRequestController : ApiController
{
private readonly ICommandHandler<OsiRequest> _osiRequestSaveHandler;
public SaveNewOsiRequestController(ICommandHandler<OsiRequest> osiRequestSaveHandler)
{
_osiRequestSaveHandler = osiRequestSaveHandler;
}
}
下面是我如何将具体的 类 注入控制器:
kernel.Bind(typeof (IValidator<>)).To(typeof (OsiRequestValidator));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (SaveOsiRequestCommandHandler))
.WhenInjectedInto(typeof (ValidationCommandHandlerDecorator<>));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto(typeof(SaveNewOsiRequestController));
代码运行良好,没有任何问题。然而,问题是如何使用 Ninject 对另一个控制器进行相同的注入?为了简单起见,我想像这样将 Another Validator
和 Another CommandHandler
注入 Another Controller
:
kernel.Bind(typeof (IValidator<>)).To(typeof (ANOTHER_Validator));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ANOTHER_CommandHandler))
.WhenInjectedInto(typeof (ValidationCommandHandlerDecorator<>));
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto(typeof(ANOTHER_RequestController));
这里的问题是您正在进行通用绑定,但您是在特定情况下使用它。当您希望泛型绑定适用于该绑定的所有情况(或者至少是您可以有条件地绑定到的所有情况)时,您只想使用泛型绑定。
为此,您可以轻松地做到这一点:
kernel.Bind<IValidator<OsiRequest>>().To<OsiRequestValidator>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<SaveOsiRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<OsiRequest>>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<ValidationCommandHandlerDecorator<OsiRequest>()
.WhenInjectedInto<SaveNewOsiRequestController>();
那么每增加一个就是:
kernel.Bind<IValidator<DifferentRequest>>().To<DifferentValidator>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<DifferentRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<DifferentRequest>>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<ValidationCommandHandlerDecorator<DifferentRequest>()
.WhenInjectedInto<DifferentRequestController>();
您可以更改最后一个绑定,这样您就不必为每种类型声明它:
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto<ApiController>();