从依赖于其他方法的返回结果的方法中删除重复代码

时间:2011-09-23 11:32:13

标签: c# asp.net-mvc refactoring code-duplication

在控制器类的ASP.NET MVC应用程序中,我有一个方法

        protected ActionResult VerifySanctions(string sanction)
        {

        bool hasSanctions = this.qualificationSanctionsService.HasSanctions(sanction);


        if (hasSanctions)
        {                               
              return this.RedirectToAction("Index", "Home");

        }
        return null;
        }

此方法包含来自不同控制器的不同其他操作的通用逻辑 我决定为控制器创建一个基类,它将保留此方法并从中派生出其他控制器。但问题在于,当我从派生类操作调用此方法时,我需要验证是否为null,然后返回结果,例如

    public ActionResult RegistrationTracking(EncryptedId sourceId)
    {
        // some code
        var registration = learnerRegistrationService.Get(sourceId);

        var result = this.VerifySanctions(registration.Qualification);
        if (result != null)
        {
            return result;
        }
         // some code
        return this.View();

    }

在其他操作中,我需要验证结果是否再次为空

    public ActionResult Index(EncryptedId achievableVersionId)
    {
        var achievableVersion = achievableVersionService.Get(achievableVersionId);

        var verificationResult = this.VerifySanctions(achievableVersion);
        if (verificationResult != null)
        {
            return verificationResult;
        }          

         //some code
        return View();
    }

我想避免重复代码,任何人都可以通过示例或建议帮助我吗? 我需要的是,如果不通过方法验证通过验证验证()然后什么也不做,否则返回行动结果。

更新动作可能会像这样

        public ActionResult ModifyUnits(EncryptedId sourceId)
    {
        var registration = learnerRegistrationService.Get(sourceId);

        Check.Require(registration != null);
        Check.Require(registration.Enrolment != null, "QualificationEnrolment is null - RegistrationAmendments / ModifyUnits");
        var result = this.VerifySanctions(registration.Qualification, LookupOrganisationAchievableStatus.Sanction3);
        if (result != null)
        {
            return result;
        }

        if (!this.organisationAchievableService.IsAccessibleAchievableVersion(registration.Qualification.Id, this.GetOrganisationId()))
        {
            return new PopupFormResult().Notify(MUI.PleaseSelectACentre);
        }

        SetEnrolmentViewData(registration.Enrolment);

        ViewData["registrationId"] = sourceId;
        var isComposite = registration.Enrolment.IsComposite();
        ViewData["isComposite"] = isComposite;

        this.SetSelectedUnitsViewData(registration, isComposite);

        this.SetSelectedQualificationUnitsViewData(isComposite, registration);

        return this.PartialView("ModifyUnits", new List<UnitDisplay>());
    }

提前致谢

3 个答案:

答案 0 :(得分:1)

这应该有效:

 public ActionResult RegistrationTracking(EncryptedId sourceId)
    {
        var registration = learnerRegistrationService.Get(sourceId);                    
        return VerifySanctions(registration.Qualification, View());
    }

    private ActionResult VerifySanctions(Sanction sanction, ViewResult view)
    {
        bool hasSanctions = this.qualificationSanctionsService.HasSanctions(sanction);

        if (hasSanctions)
        {
            return RedirectToAction("Index", "Home");
        }

        return view;
    }

答案 1 :(得分:1)

您可以在所有控制器扩展的默认控制器中添加方法 嵌入这个逻辑。例如,

private ActionResult ViewWithSanction(string sanction, ActionResult default)
{
  bool hasSanctions = this.qualificationSanctionsService.HasSanctions(sanction);
  if (hasSanctions) {                               
    return this.RedirectToAction("Index", "Home");
  }
  return default;
}

然后你的方法看起来像

public ActionResult RegistrationTracking(EncryptedId sourceId)
{
    var registration = learnerRegistrationService.Get(sourceId);
    return ViewWithSanction (registration.Qualification, View());
}

public ActionResult Index(EncryptedId achievableVersionId)
{
    var achievableVersion = achievableVersionService.Get(achievableVersionId);
    return ViewWithSanction (achievableVersion, View());
}

答案 2 :(得分:1)

传递Func<ActionResult>为假时执行的hasSanctions

private ActionResult VerifySanctions(Sanction sanction, Func<ActionResult> whenNoSanctions)
{
    bool hasSanctions = this.qualificationSanctionsService.HasSanctions(sanction);

    if (hasSanctions)
    {
        return RedirectToAction("Index", "Home");
    }

    return whenNoSanctions();
}

并致电

 return VerifySanctions(registration.Qualification, ()=> { //any code that returns ActionResult });

<强>更新

您可以将非空零件提取为单独的方法

private ActionResult WhenNoSanctions()
{
        if(!this.organisationAchievableService.IsAccessibleAchievableVersion(registration.Qualification.Id, this.GetOrganisationId()))
        {
            return new PopupFormResult().Notify(MUI.PleaseSelectACentre);
        }

        SetEnrolmentViewData(registration.Enrolment);

        ViewData["registrationId"] = sourceId;
        var isComposite = registration.Enrolment.IsComposite();
        ViewData["isComposite"] = isComposite;

        this.SetSelectedUnitsViewData(registration, isComposite);

        this.SetSelectedQualificationUnitsViewData(isComposite, registration);

        return this.PartialView("ModifyUnits", new List<UnitDisplay>());
}

并将VerifySanctions称为

 return VerifySanctions(registration.Qualification, WhenNoSanctions);