Dependency injection - Place logic in the overloaded constructor?

I'm a little confused about what, exactly, you are trying to accomplish. If I am to assume you need to provide Smtp emailers, and you are using IoC, then you should be creating and wiring up your entire object graph with the IoC framework. By that, I mean that you would configure your IoC framework to create the SmtpClient, which it then creates the SmtpClientWrapper with, finally creating the SmtpEmailProvider with.

You should not need to put any dependency-creation logic in the SmtpEmailProvider constructor.

I'm a little confused about what, exactly, you are trying to accomplish. If I am to assume you need to provide Smtp emailers, and you are using IoC, then you should be creating and wiring up your entire object graph with the IoC framework. By that, I mean that you would configure your IoC framework to create the SmtpClient, which it then creates the SmtpClientWrapper with, finally creating the SmtpEmailProvider with.

You should not need to put any dependency-creation logic in the SmtpEmailProvider constructor. Here is an example with Castle Windsor, given the code you provided: 127.0.0.1 25 ${smtpClient} ${smtpClientWrapper} With the above Windsor configuration, you can simply create your IMessageProvider like so: public IMessageProvider LocateProviderByName(string providerName) { return IoC. Resolve(providerName); } var messageProvider = LocateProviderByName("smtpProvider"); The key point here is to use the IoC container for what it is: a dependency creation and management system that can, and should, create full object graphs for you.

This alleviates the problem you have with too much dependency management logic in a constructor.

1 for giving me new ideas of how to do things. But say that the logic there isn't just something that can be specified with IoC. Like that I for example need to be able to fetch Host/Port from some other setting repository depending on some parameter.

Where would be the correct place to put the logic? – Allrameest Jun 26 '09 at 8:11 1 If you need that kind of capability, I would create another provider or factory that can be injected, rather than an instance of what you need. For example, it seems like you need to dynamically assign a Host, Port, and Credential to the SmtpClient.

I would create an SmtpClientFactory that is then injected into the SmtpClientWrapper. The SmtpClientWrapper may then create its internal instance of the SmtpClient internally (and cache it if need be. ) The key thing here, to maintain mockability, is to make sure your SmtpClientFactory has virtual methods.

– jrista Jun 26 '09 at 8:13 Ok! A factory was one thing I had in mind. I'll go with that!

:) – Allrameest Jun 26 '09 at 8:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions