Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net MVC Actions - Separation of Concerns/Single Responsibility Principle
    text
    copied!<p>In computer science we've been taught that each method should do one thing and one thing only. I'm a little confused then that we see MVC actions like the following <a href="http://www.nerddinner.com/" rel="noreferrer">given as examples of good practice</a>:</p> <pre><code> [AcceptVerbs(HttpVerbs.Post), Authorize] public ActionResult Edit(int id, FormCollection collection) { Dinner dinner = dinnerRepository.GetDinner(id); if (!dinner.IsHostedBy(User.Identity.Name)) return View("InvalidOwner"); try { UpdateModel(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id=dinner.DinnerID }); } catch { ModelState.AddModelErrors(dinner.GetRuleViolations()); return View(new DinnerFormViewModel(dinner)); } } </code></pre> <p>Basically this piece of code provides a lot of functionality:</p> <ol> <li>Defines how to access the Action - Post only</li> <li>Defines who can access the Action - Authorize</li> <li>Accesses persistence mechanism - dinnerRepository</li> <li>Accesses state information - (User.Identity.Name)</li> <li>Converts NameValueCollection to strongly typed object - UpdateModel()</li> <li>Specifies 3 possible ActionResults and content for each - InvalidOwner/Details/Edit views</li> </ol> <p>To me this seems like too many responsibilities for one method. It is also a fairly simple action ie it doesn't deal with common scenarios like:</p> <ol> <li>Checking business rules - "Never trust user input"</li> <li>Navigation paths - On successful save always returns to "Details"</li> <li>Different return types - Someone wants to call "Edit" from a grid and needs a JsonResult?</li> <li>Better error handling - YSOD if the database is not accessible during GetDinner(id)</li> <li>Constructing additional view data - SelectLists for Drop down lists</li> </ol> <p>Not too mention the amount of testing required around this single method i.e. mocking/faking for FormCollection/UserIdentity/Authorization Provider/Repository/etc. </p> <p>My question is how do we avoid cramming so much stuff into our controller actions? </p> <p>I tend to think <a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/10/23/our-opinions-on-the-asp-net-mvc-introducing-the-thunderdome-principle.aspx" rel="noreferrer">"opinions"</a> are a great concept, especially the "Thunderdome Principle". While I have great respect for the guys involved with building <a href="http://fubumvc.pbworks.com/" rel="noreferrer">FubuMVC</a> and their reasons behind doing so, I need something I can use right now. </p> <p>Edit - Well it seems I was after something like this - <a href="http://flimflan.com/blog/SampleOpinionatedController.aspx" rel="noreferrer">Opinionated Controller</a>. I'll need to examine it further as it's for MVC Preview 5 so i may need to update it myself.</p>
 

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