Today I've got this class:
public class SmtpEmailProvider : IMessageProvider
{
private readonly SmtpClientWrapper _smtpClientWrapper;
public SmtpEmailProvider(SmtpClientWrapper smtpClientWrapper)
{
_smtpClientWrapper = smtpClientWrapper;
}
To be able to mock the SmtpClient, I've wrapped it like this:
public class SmtpClientWrapper
{
private readonly SmtpClient _smtpClient;
public SmtpClientWrapper(SmtpClient smtpClient)
{
_smtpClient = smtpClient;
}
public virtual void Send(MailMessage msg)
{
if (_smtpClient == null) throw new InvalidOperationException("SmtpClient must be passed to the constructor before calling Send.");
_smtpClient.Send(msg);
}
}
Right now I can do this to initiate the SmtpEmailProvider class, and place the SmtpClient logic there:
public IMessageProvider LocateProviderByName(string providerName)
{
var client = new SmtpClient
{
Host = "127.0.0.1",
Port = 25
};
client.Credentials = new NetworkCredential("...", "...");
return new SmtpEmailProvider(new SmtpClientWrapper(client));
}
But I want to replace that with:
public IMessageProvider LocateProviderByName(string providerName)
{
return IoC.Resolve<IMessageProvider>(providerName);
}
Then I need to place the logic in the constructor with no parameters. But I get the feeling that I'm doing to much in the contructor then.
Is there any other way to do it?