我如何清理这个控制器

本文关键字:控制器 何清理 | 更新日期: 2023-09-27 18:15:14

如果用户忽略表单,直接进入网页,我希望今天的日期为默认值。

但是我觉得在这两者之间复制粘贴我所有的代码行是很糟糕的代码,当唯一改变的是日期。

如何清理这段代码?

First Results()我所做的就是设置objdate1。DateStart自己。
在[HttpPost]我得到objdate1。从表单开始。

    public ActionResult Results()
    {
        Date1 objdate1 = new Date1();
        objdate1.DateStart = DateTime.Now;
        var DataContext = new BalanceDataContext();

        DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);
        //Tons of Code omitted Here ---------------------------

        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });
    }
    [HttpPost]
    public ActionResult Results(Date1 objdate1)
    {
        var DataContext = new BalanceDataContext();

        DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);
        //Exact same Code omitted Here ---------------------------

        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });
    }

我如何清理这个控制器

将参数设为空并删除无参数方法

[HttpPost]
public ActionResult Results(Date1? objdate1)
{
var DataContext = new BalanceDataContext();
if (!objdate1.HasValue){
    objdate1 = new Date1();
    objdate1.DateStart = DateTime.Now;
}
DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);
//Exact same Code omitted Here ---------------------------

ViewBag.Metric = 1;
ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
ViewBag.Title2 = "% of Electric Estimated";
ViewBag.Descript = "Percentage of estimated electric bills per Month.";
return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });

}

来自c# Freenode的用户RandomStrangler给了我答案:

"将逻辑放在单独的类中,然后从两个操作中调用它。"

所以我创建了一个名为Query's的单独类,并这样做:
    public ActionResult Results()
    {
        Date1 objdate1 = new Date1();
        objdate1.DateStart = DateTime.Now;
        Querys estview = new Querys();
        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(estview.estimatedbills(objdate1.DateStart));
    }
    [HttpPost]
    public ActionResult Results(Date1 objdate1)
    {
        Querys estview = new Querys();
        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(estview.estimatedbills(objdate1.DateStart));
    }

好多了。