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

发布于 2024-12-06 04:17:35 字数 2670 浏览 1 评论 0原文

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

        protected ActionResult VerifySanctions(string sanction)
        {

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


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

        }
        return 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();
    }

并且我想避免重复代码,任何人都可以帮忙我举例或建议? 我需要的是,如果没有通过方法VerifySanction() 通过验证,则不执行任何操作,否则返回操作结果。

更新操作可以像这样

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

提前致谢

In ASP.NET MVC application in controller class I have a method

        protected ActionResult VerifySanctions(string sanction)
        {

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


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

        }
        return null;
        }

This method contains common logic from different other actions from different controller
I decided to create a base class for controller that will keep this method and derive other controller from it. But problem is in when I call this method from derived class actions I need to verify if not null and then return the result for example

    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();

    }

in other actions I need to verify if result not null again

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

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

         //some code
        return View();
    }

And I want to avoid duplication code, can anyone help me with example or advise?
What I need that if don't pass verification by method VerifySanction() then do nothing, else return Action result.

UPDATE the actions could like this

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

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽你眉间 2024-12-13 04:17:35

这应该有效:

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

This should work:

 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;
    }
清风无影 2024-12-13 04:17:35

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

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

You could add a method in a default controller extended by all your controller
to embed this logic. For example,

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

And then your method could looks like

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());
}
§对你不离不弃 2024-12-13 04:17:35

传递当 hasSanctions 为 false 时将执行的 Func

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

UPDATE:

您可以将非空部分提取为单独的方法

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

并调用验证制裁为

 return VerifySanctions(registration.Qualification, WhenNoSanctions);

Pass the Func<ActionResult> that will execute when hasSanctions is false

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

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

    return whenNoSanctions();
}

And calling

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

UPDATE:

You could extract your not null part as separate method

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

And call VerifySanctions as

 return VerifySanctions(registration.Qualification, WhenNoSanctions);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文