I would say something like this:
public interface INotifier
{
void SendEmailNotification(string subject, string body, string emailAddress);
}
public class Notifier : INotifier
{
public void SendEmailNotification(string subject,string body,string emailAddress)
{
var smtpClient = new SmtpClient();
var message = new MailMessage();
message.Subject = subject;
message.Body = body;
message.To.Add(new MailAddress(emailAddress));
smtpClient.Send(message);
}
}
public class TestEventHandlers
{
public INotifier Notifier { get; set; }
public TestEventHandlers()
{
Notifier = new Notifier();
}
public void OpenMarket(Page page)
{
var Id = page.Request["MarketId"];
if (id!=null)
{
var repository = new EntityRepository();
IEntity market = repository.GetById(Id);
if (market.State != "Open")
{
throw new Exception("The market is not open!");
}
else
{
market.Open();
repository.SaveChangesTo(market);
Notifier.SendEmailNotification("market open", market.ToString() + " was open.", "market@mail.com");
}
}
else
{
throw new Exception("entityId can not be null");
}
}
public void CloseMarket(Page page)
{
var Id = page.Request["MarketId"];
if(id!=null)
{
var repository = new EntityRepository();
IEntity market = repository.GetById(Id);
if (market.State == "Close")
{
throw new Exception("The market is already close!");
}
else
{
market.Close();
repository.SaveChangesTo(market);
Notifier.SendEmailNotification("market closed", market.ToString() + " has been closed.", "market@mail.com");
}
}
else
{
throw new Exception("entityId can not be null");
}
}
}
In this, your INotifierdefault is used by your normal implementation by the constructor, but can be redefined if necessary using accessories. If you prefer to always introduce your dependencies, even if you are not testing, you can simply add arguments to the constructor.
Hope this helps.
source
share