Twitter LinkedIn Github


I had a short talk yesterday on ASP.NET MVC. For someone that has been developing using ASP.NET and seeing ASP.NET MVC for the first time, has an immediate reaction of "we're going back in time, we're heading back to spaghetti code". And it's understandable. What would be your first impression if you saw something like the code below in an ASPX file?

   1: <%foreach (Customer customer in ViewData.Model)
   2:    >
   3: <tr>
   4:     <td style="width: 98px">
   5:         <%=Html.ActionLink(customer.Firstname, "Edit", new { id = customer.Id}) %>
   6:         &nbsp;</td>
   7:     <td style="width: 112px">
   8:         <%=customer.Lastname %>

We've moved on several years and the best we could do was replace a for loop with a foreach?

But ASP.NET MVC isn't about going back to unreadable code. It's the exact opposite. MVC is about separation of concerns. For those not familiar with the MVC Movie, three actors participate: The Model, the View and the Controller.

Model: The business model, the data, the domain logic, whatever you want to call it. It's responsibility is the model and everything to do with the model.

View: The user interface. It displays and collects information. It's entire responsibility is to display the information and to receive information from the user.

Controller: Middleman. It's job is to coordinate between the Model and the View. It gets information from the model and provides it to the view. It receives information from the view and passes it back to the model. It takes care of any logic that has to do with both parties involved.

To put it in other terms, the Model is the property owner, the View is the guy that wants to buy the house. The Controller is the notary. It coordinates the actions between the two parties involved. It's not there to make decisions for the owner or for the buyer. It's an neutral entity (that gets paid way too much IMHO, but that's beside the point). Without one of the parties, the notary has no reason for existing.

[Note for the purists: this is not the strict MVC architecture defined in Smalltalk where the View had access to the Model]

Going back to the previous code and looking at what it's actually doing, we see that it's only looping through a list of customers and display the information. In other words, it is not stepping out of it's boundaries. It's not trying to take on other responsibilities. However, having said that, nothing stops us from doing other things in the code. If we have access to the model, we can start doing some nasty things by delegating the responsibility of a controller to a view and let the view start deciding when and how something should be displayed.

The developer: the forth actor

There are really four actors in ASP.NET MVC(D). You need to apply good design principles and truly understand what single responsibility and separation of concerns means. Don't think for a moment that you are safe just because you're using a framework, pattern or architecture.