This is a mirror of official site: http://jasper-net.blogspot.com/

Controller Action Design in MVC

| Sunday, August 22, 2010
Validate, Act, Translate, and Respond. That’s about it.
I’ve been trying to come up with a nice acronym for how to structure code in Actions for some time now, and this is the best I have managed. I wish it spelt a nice word – so if you’ve a better suggestion shout up. What I’m trying to say is – “don’t stuff all your business logic and all sorts of other code into your controller” and, “follow a common, clear pattern to writing your controller actions”. This is similar to the AAA acronym for unit testing; Arrange, Act, and Assert; which really helps developers focus when writing tests. Therefore, my slightly awkward VATR acronym suggests that in an MVC action you;

1.       Validate; check the input, either through automatic model binding, explicit validation checks, or a combination of both.
2.       Act; apply the business logic – save, fetch data, etc.
3.       Translate; convert entities returned from the business logic into View Model entities. See this post if you want to understand the motivation for this further.
4.       Respond; render a view, or redirect to another action, etc.

This does not include “inspect database” or “loop through all posted data and….” steps, because I believe they should be encapsulated in the business logic, and therefore not in your controller. It also doesn’t include authorisation, error handling, or other concerns that can be handled with filters etc. Let’s take some example code and see what I mean.

Example 1: The Simple Case

Taking a typical Index action, perhaps for a blog site;

public ActionResult Index()
{
   var articles = _logic.GetLatestArticles();

   var model = new HomeIndexViewModel
   {
       Articles = Mapper.Map(articles),
       Today = DateTime.Today,
       Username = UserInfo.Username
   };

   return View(model);
}

In this example, there is no data input directly to the action, so Validate is notable by it’s absence. Anything the action uses should be validated of course, such as the Username retrieved from a helper class; but that validation is encapsulated elsewhere. Next, we Act -  by calling the GetLatestArticles method in my business logic. This returns a list of article business entities.
I don’t want to pass these business entities to the view (why?), so we Translate them into a View Model using a helpful Mapper class. And finally, we Respond with a command to render a view using our View Model.

Example 2: A POST Action

Upping the complexity slightly, let’s consider a POST that needs to save some data;

[HttpPost]
public ActionResult Display(HomeDisplayViewModel model)
{
   if (ModelState.IsValid)
   {
       _logic.UpdateArticle(model.Article);
       return RedirectToAction("Index");
   }
   else
       return View(model);
}

Read more: UK Premier Support for Developers

Posted via email from .NET Info

0 comments: