Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove repeated code in my actions?

I have the following code repeated several times in a mvc app.

    public ActionResult AnAction(int Id)
    {           
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        // do stuff here
        ....
        return ....;
    }

So far this pattern is used 4 times and it's getting ugly. What is the best way to refactor it?

Edit:
A couple of example usages

    public ActionResult Claim(int Id)
    {           
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        return View("Claim", claim);
    }

    public ActionResult MedicalPV(int Id)
    {
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        return PartialView(claim.MedCerts.AsQueryable<MedCert>());
    }

Typically I am requiring access to the object in the view. This particular code is only used in one controller, but I am may need something similar in other controllers with different objects and views.

like image 773
SeanX Avatar asked Dec 30 '25 08:12

SeanX


1 Answers

If all of the actions require a claim, then you could try to retrieve it in OnActionExecuting and set the Result to the ViewResult on failure. If only some actions, require it, perhaps, an ActionFilter that checks to make sure that a claim is available before executing the method and, if not, sets the proper View.

private Claim Claim { get; set; }

public override void OnActionExecuting( ActionExecutingContext context )
{
    this.Claim = GetClaim( int.Parse( context.RouteData["id"] ) );
    if (this.Claim == null)
    {
        context.Result = View( "ClaimNotFound" );
    }
}

OR

public class RequiresClaimIdAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting( ActionExecutingContext context )
    {
         var claim = GetClaim( int.Parse( context.RouteData["id"] ) );
         if (claim == null)
         {
              context.Result = new ViewResult
              {
                   ViewName = "ClaimNotFound",
                   ViewData = context.Controller.ViewData
              };
         }
         else
         {

              var property = context.Controller
                                    .GetType()
                                    .GetProperty( "Claim",
                                                   BindingFlags.Public
                                                   | BindingFlags.NonPublic
                                                   | BindingFlags.Instance);
              if (property != null)
              {
                   property.SetValue(context.Controller,claim);
              }
         }
    }
}

[RequiresClaimId]
public ActionResult AnAction( int id )
{
    this.Claim.Updated = DateTime.Now;
    ...
}
like image 50
tvanfosson Avatar answered Jan 02 '26 00:01

tvanfosson