Remove duplication code from methods that depend on return result of other method
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.GetOrganisat开发者_如何学JAVAionId()))
{
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
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;
}
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());
}
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);
精彩评论