装饰图案与Ninject

时间:2015-03-28 20:29:10

标签: c# dependency-injection ninject service-layer

我正在尝试应用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为另一个控制器进行相同的注射?为简单起见,我想将另一个Validator和另一个CommandHandler注入另一个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));

1 个答案:

答案 0 :(得分:1)

这里的问题是您正在进行通用绑定,但是您在特定情况下使用它。如果希望将通用绑定应用于该绑定的所有情况(或至少可以有条件地绑定到的所有情况),则只需要使用通用绑定。

为此,您可以轻松地执行此操作:

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>();