我想重構下面的代碼,我該如何注入通知處理程序?最小的原始代碼更改和最佳重構(如有必要)。如何最好地重構此C#代碼?
public class TestEventHandlers
{
public TestEventHandlers() { }
public void OpenMarket(Page page)
{
var Id = page.Request["MarketId"];
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);
var smtpClient = new SmtpClient();
var message = new MailMessage();
message.Subject = "market open";
message.Body = market.ToString() + " was open.";
message.To.Add(new MailAddress("[email protected]"));
smtpClient.Send(message);
}
}
public void CloseMarket(Page page)
{
var Id = page.Request["MarketId"];
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);
var smtpClient = new SmtpClient();
var message = new MailMessage();
message.Subject = "market closed";
message.Body = market.ToString() + " has been closed.";
message.To.Add(new MailAddress("[email protected]"));
smtpClient.Send(message);
}
}
}
我已經重構它像下面 -
public class TestEventHandlers
{
public TestEventHandlers() { }
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);
SendEmailNotification("market open", market.ToString() + " was open.", "[email protected]");
}
}
else
{
throw new Exception("Id can not be null");
}
}
private static 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 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);
SendEmailNotification("market closed", market.ToString() + " has been closed.", "[email protected]");
}
}
else
{
throw new Exception("Id can not be null");
}
}
}
你可能想合併'CloseMarket'和'OpenMarket'。您只能將其狀態更改爲關閉或打開。一個參數可以將2種方法減少到1. – Badiparmagi
我會與Badipamagi的建議一起使用。也請開始添加空檢查。 SendNotification中也可以使用相同的新參數。如果你沒有在其他地方使用發送通知,你可以合併它,但從測試點,並保持分離的擔憂,你可以保持原樣。 –
也添加了null檢查,但它並不是一個好主意,因爲它對現在使用兩種方法的其他類和方法進行了巨大的重構,即「OpenMarket(Page page) and CloseMarket(Page page)' – Neo