How to declare a chain of responsibility using decorators in Ninject?

I would like to declare a chain of responsibility using decorators in Ninject.

Has anyone done this before?

Thanks.

+4
source share
2 answers

In the new ninject 2.0 syntax, you can run the following syntax:

Bind<IEmailSender>().To<LoggingEmailSender>(); Bind<IEmailSender>().To<SmtpClientEmailSender>().WhenInjectedInto<LoggingEmailSender>(); 

Just stumbled upon it myself and found that it was a way to do it, since ForMembersOf was removed in ninject 2.0

+13
source

Assuming I understand the question correctly, one approach looks something like this:

 Bind<IEmailSender>().To<LoggingEmailSender>(); Bind<IEmailSender>().To<SmtpClientEmailSender>().ForMembersOf<LoggingEmailSender>(); 

The LoggingEmailSender class will have a constructor, for example:

 LoggingEmailSending(IEmailSender sender) 

This should make you a decorator quite easily - just remember that without using attributes you are limited to one constructor.

+2
source

All Articles