Blog Archives

Focusing on the controller’s responsibility

This morning I came across an interesting article about Controller responsibility: http://mhinze.com/focusing-on-the-controllers-responsibility/

This is the excerpt:

A heavyweight controller

public RedirectToRouteResult Ship(int orderId)
{
   User user = _userSession.GetCurrentUser();
   Order order = _repository.GetById(orderId);

   if (order.IsAuthorized)
   {
      ShippingStatus status = _shippingService.Ship(order);

      if (!string.IsNullOrEmpty(user.EmailAddress))
      {
         Message message = _messageBuilder
            .BuildShippedMessage(order, user);

         _emailSender.Send(message);
      }

      if (status.Successful)
      {
         return RedirectToAction("Shipped", "Order", new {orderId});
      }
   }
   return RedirectToAction("NotShipped", "Order", new {orderId});
}

According to the author, that controller does a lot of things, so he suggests a refactoring techniques called “Refactor Architecture by Tiers. It directs the software designer to move processing logic out of the presentation tier into the business tier.”

Here is how the controller is refactored by moving the logic for shipping an order to an OrderShippingService, our action is much simpler.

A simpler action after refactoring architecture by tiers
public RedirectToRouteResult Ship(int orderId)
{
   var status = _orderShippingService.Ship(orderId);
   if (status.Successful)
   {
      return RedirectToAction("Shipped", "Order", new {orderId});
   }
   return RedirectToAction("NotShipped", "Order", new {orderId});
}

I must admit that I have implemented that so-called _heavyweight_ controller when developing website using ASP.NET MVC and I rather call it a ‘fat’ controller which violates Single Responsibility Principal since it does a lot of things. My solution is somewhat familiar with the author: moved the logic to Service or Business layer. This will keep the controller ‘thin’ and concentrates on its responsibility: a coordinator between View and Model which intercepts user request, asking  business layer to perform business logic, pull data from Model and choose a right View to return to client.

And it takes time, even blood, to absorb.

Lesson learned from building Bua An VIET Website

There are many things worth noting after finishing the phase 1 of Bua An VIET:

– This is the first time I built a website using ASP.NET MVC (1.0), although I have some experiences with ASP.NET Web Forms, there still are too many things to learn, especially, implement custom model binder to fetch complex object graph, how to do ajax with jQuery, how routing works and how to make it works as expected via routing configuration.

– Working with NHibernate has been more and more of pleasure. Fluent NHibernate is handy and it saves me time to debug mapping error. S#arp Architect is a great open source web framework to be employed. I really like the way that NHibernate Session is managed, although I am not applying Repository pattern like its reference implementation (Northwind sample).

jQuery is awesome.

– It took me a lot of time to deal with CSS because of lacking deep understanding on it or my knowledge is outdated. Knowing many resources of CSS and jQuery helped me out, but it still took me time.

– I must admit that building web development requires good graphic design skill, and I don’t have one. Wandering over internet to choose a right template is a way to do, but it is also time consuming and when I want to change the layout/template with a new one, I nearly have to start over with another messy html and CSS.

– A number of design patterns and design principals have been being applied. This is where I learnt a lot. Some of design pattern which I always think it must be applied and/or some principals I always think it is true have becomes not suitable and/or over complicated, at least to me. I learnt the fact that “Theory  is  just pink, and trees are always green”

– We are planning to add more features to the site, also an upgrade to ASP.NET MVC 2 is planned. This will bring more useful information to the user and more fun to me.