Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update 2</strong>: I didn't realize that this question was about MVC. Sorry about that. The answer below is still valid from a more generic point of view anyway.</p> <ul> <li><p>The first refactoring I would do is taking the <code>GetClaim</code> call out of the method. Looks like <code>AnAction</code> could not (and, therefore should not) depend on <code>GetClaim</code>. It should directly receive a <code>Claim</code> instead;</p></li> <li><p>Secondly, I would apply the same rule to the <code>View</code> call. Instead, <code>AnAction</code> should simply not allow a null <code>Claim</code>.</p></li> </ul> <p>The code would look like this:</p> <pre><code>public ActionResult AnAction(Claim claim) { if (claim == null) throw new ArgumentNullException("cliam"); // do stuff here .... return ....; } </code></pre> <p>You might say I simply moved out the responsibility of handling null <code>Claim</code>s to another place. And that's exactly what I suggest you to do. The original problem was that all your several <em>action</em> methods were having too much responsibility. I'm only applying the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow noreferrer">Single responsibility principle</a>.</p> <hr> <p><strong>Update 1</strong>: After making the refactorings I suggested, you could substitute all calls to the <em>action</em> methods by a call to the following method. It might not be the solution you're looking for, but it looks like a good start (at least to me).</p> <pre><code>public static ActionResult InvokeAction(Func&lt;Claim,ActionResult&gt; action, int id) { var claim = GetClaim(Id); if (claim == null) return View("ClaimNotFound"); return action(claim); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload