我如何最好地重构这个C#代码?

时间:2016-11-05 05:39:19

标签: c# asp.net webforms

我想重构以下代码,如何注入通知处理程序?最小的原始代码更改,并在必要时进行最佳重构。

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("market@mail.com"));

            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("market@mail.com"));

            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.", "market@mail.com");
            }
           }
          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.", "market@mail.com");
            }
        }
           else
            {
                throw new Exception("Id can not be null");
            }
       }
    }

2 个答案:

答案 0 :(得分:1)

试试这个@Neo

IN (1,2,3)

答案 1 :(得分:1)

我会选择这样的事情:

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");
        }
    }       
}

在此,INotifier默认为构造函数的常规实现,但在需要时可以使用访问器覆盖。如果你更愿意总是注入你的依赖项,即使没有测试,你也可以只为构造函数添加参数。

希望这有帮助。