从依赖于其他方法的返回结果的方法中删除重复代码
在控制器类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该有效:
This should work:
您可以在由所有控制器扩展的默认控制器中添加一个方法
嵌入这个逻辑。例如,
然后你的方法可能看起来像
You could add a method in a default controller extended by all your controller
to embed this logic. For example,
And then your method could looks like
传递当
hasSanctions
为 false 时将执行的Func
并调用
UPDATE:
您可以将非空部分提取为单独的方法
并调用验证制裁为
Pass the
Func<ActionResult>
that will execute whenhasSanctions
is falseAnd calling
UPDATE:
You could extract your not null part as separate method
And call VerifySanctions as