【问题标题】:Decorator pattern with Ninject带有 Ninject 的装饰器模式
【发布时间】:2015-06-02 01:17:53
【问题描述】:

我正在尝试应用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));

【问题讨论】:

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


    【解决方案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>();
    

    【讨论】:

    • 非常感谢埃里克。这是一个救生员。就一个问题。我是否遵循正确的方法?是否将特定的具体类注入通用接口,从架构的角度来看是否正确?
    • @MehrdadKamelzadeh - 好吧,当您在特定情况下需要特定实例时,您没有太多选择。如果您可以找到一种仅使用接口中的类型调用通用实现的方法,则可以简化此操作,但是您的实现似乎没有这样做。此外,您可以重用最后一个绑定,因为这是通用的……请参阅我的更新。这利用了 Ninject 在使用 .WhenInjectedInto() 时会查看基类这一事实@
    • @MehrdadKamelzadeh - 但其他两个必须保持特定,因为无法自动将泛型类型映射到特定类型。
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多