Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a chain of responsibility using decorators in Ninject?

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

Has anyone done that before?

Thanks.

like image 296
Adriano Machado Avatar asked Dec 07 '22 08:12

Adriano Machado


2 Answers

In the new ninject 2.0 syntax you can accomplish this by the following newer syntax:

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

Just ran into this myself and found that was the way to do it as ForMembersOf has been removed in ninject 2.0

like image 140
Sean Chambers Avatar answered Dec 21 '22 22:12

Sean Chambers


Assuming I'm understanding the question properly, one approach is something like this:

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

The LoggingEmailSender class would have a constructor something like:

LoggingEmailSending(IEmailSender sender)

This should get you a decorator easily enough - just remember that without using attributes, you're kinda limited to a single constructor.

like image 26
chrisb Avatar answered Dec 21 '22 23:12

chrisb