Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject equivalent for SimpleInjector RegisterDecorator method

I have the following code in Simple Injector IoC container:

container.RegisterDecorator(typeof(ICommandHandler<>),
    typeof(ValidationCommandHandlerDecorator<>));

I need to translate this to Ninject equivalent. I've read that the Decorator pattern in Ninject is done via the WhenInjectedInto method, but the whole biding requires like 3 parameters like here:

Bind<IRepository>().To<SimpleRepository>
    .WhenInjectedInto<AdvancedRespository>();

This method in Simple Injector takes just 2, so could you tell me please what I'm missing here?

like image 311
user2327105 Avatar asked Oct 22 '22 11:10

user2327105


2 Answers

I think there is no direct equivalent to the RegisterDecorator functionality of SimpleInjector. If I understand it right, this defines that whenever you request an ICommandHandler you will get a ValidationCommandHandlerDecorator returned that decorates some default ICommandHandler. In Ninject you need to do this like you already did. At least I'm not aware of any functionality or extension that directly provides that mechanism.

See also this question How the binding are done with decorators using Ninject?

like image 72
treze Avatar answered Nov 02 '22 23:11

treze


After reading about decorators in Simple Injector, I don't really see how it's any different from a normal injection. Why can't you just do this?

kernel.Bind(typeof(ICommandHandler<>))()
      .To(typeof(ValidationCommandHandlerDecorator<>))

If you need to control that several types are injected based on the type of object, then you would use the .WhenInjectedInto()

like image 20
Erik Funkenbusch Avatar answered Nov 02 '22 23:11

Erik Funkenbusch