Reputation: 1784
I am trying to apply this approach to make a validation wrapper. I have created the following Interfaces and I am using Ninject as my DI Container
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;
}
}
And the following is how I am injecting the concrete classes into the controller:
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));
the code is working fine without any problem. However the question is how can I do the same injection for another controller with Ninject? To make it simple I want to inject Another Validator
and Another CommandHandler
to Another Controller
like this:
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));
Upvotes: 2
Views: 1347
Reputation: 93444
The issue here is that you're doing generic binding, but you are using it in specific situations. You only want to use generic binding when you want it to apply to all situations of that binding (or at least all the situations you can conditionally bind to).
For this, you can just as easily do this:
kernel.Bind<IValidator<OsiRequest>>().To<OsiRequestValidator>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<SaveOsiRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<OsiRequest>>();
kernel.Bind<ICommandHandler<OsiRequest>()
.To<ValidationCommandHandlerDecorator<OsiRequest>()
.WhenInjectedInto<SaveNewOsiRequestController>();
Then each additional would be:
kernel.Bind<IValidator<DifferentRequest>>().To<DifferentValidator>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<DifferentRequestCommandHandler>()
.WhenInjectedInto<ValidationCommandHandlerDecorator<DifferentRequest>>();
kernel.Bind<ICommandHandler<DifferentRequest>()
.To<ValidationCommandHandlerDecorator<DifferentRequest>()
.WhenInjectedInto<DifferentRequestController>();
You could change the last binding so you wouldn't have to declare it for each type:
kernel.Bind(typeof (ICommandHandler<>))
.To(typeof (ValidationCommandHandlerDecorator<>))
.WhenInjectedInto<ApiController>();
Upvotes: 1