5

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?

Steven
  • 166,672
  • 24
  • 332
  • 435
user2327105
  • 123
  • 1
  • 3

2 Answers2

2

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?

Community
  • 1
  • 1
treze
  • 3,159
  • 20
  • 21
1

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

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 3
    The difference between between applying decorators with Simple Injector vs most other containers, is that decorators in Simple Injector are not registered themselves but become part of an existing registration (they change/intercept the real registration). In most cases this difference is irrelevant, but does make certain scenarios considerably easier. Simple Injector's users often apply decorators conditionally (based on some generic type constraint or predicate). This is something that is often much harder to do with other frameworks. It's often not impossible, but simply not as clean. – Steven Apr 29 '13 at 11:20
  • @Steven - Ninject offers Interception functionality as well, and you can use a type or predicate to control what is injected into what (using `.When()` for a predicate). I'm still failing to understand the difference here, and the phrase "not registered themselves but become part of an existing registration" seems pointless. Why would you register one thing if you intend to register a different thing? Just change your registration, that's the whole point of making registration a configuration. – Erik Funkenbusch Apr 29 '13 at 13:26
  • 4
    Functionally the interception capabilities of Ninject are much like the decorator capabilities of Simple Injector, but interception != decoration. A difference is that decorators are cleaner than interceptors, for instance because they can be written in a framework agnostic way, while an interceptor has to implement a certain (framework specific) interface. The difference might seem insignificant to you, but not all developers feel that way. That's why Simple Injector focuses on decoration instead of interception. – Steven Apr 29 '13 at 14:00