« Visual State Manager feature for a WPF project – Expression Blend Based Approach | Main | ASP.NET MVC 2 Preview 2 Released »

ASP.NET MVC – Understanding the Model-View-Controller (MVC) pattern

ASP.NET MVC is something that Microsoft started talking about approximately 2 years ago! In October 2007, Scott Guthrie presented the first preview of the ASP.NET MVC framework at PDC. Before ASP.NET MVC came into the picture, the .NET platform did not provide the building blocks to develop a web application which uses the Model-View-Controller architecture. This was something that the .NET developer community had been asking the ASP.NET support team for, since a very long time. Ever since the first preview of the framework was released, the interest in this framework has been growing, and a vast amount of sample applications, components, and so on have been released on the Internet by enthusiast bloggers and Microsoft employees.

For the sake of people who are not really clear as to what Model-View-Controller (MVC) pattern is, it would be better that we start off by describing what MVC is. MVC is basically a UI design pattern. The main purpose of this design pattern is to isolate business logic from the user interface, in order to focus on better maintainability, testability, and a cleaner structure to the application. The MVC pattern consists of three key parts: the model, the controller, and the view. Let’s delve a bit into these key parts.

Model
The model consists of some encapsulated data along with its processing logic. The model object knows all of the data that needs to be displayed. It can also define some operations that manipulate this encapsulated data. It knows absolutely nothing about the graphical user interface—it has no clue about how to display data or respond to actions that occur in the GUI. Often, model objects retrieve and store model state in a database. For example, a Customer object might retrieve information from a database, operate on it, and then write updated information back to a Customers table in a SQL Server database. Simply put, the model contains the business logic.

View
Views are the components that display the application's user interface (UI). The view object refers to the model. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Product object. Simply put, the presentation logic is located in the view component.

Controller
Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. Actually, the controller object is the interaction glue of the model and the view. Hence the controller object has to interact both with the model and the view. Simply put, the controller contains the input logic.

If you have now understood what exactly do we mean by terms like Model, View and Controller, you would be able to appreciate the fact that the MVC pattern ensures that there is a clean separation of different aspects of an application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic is in the view. Input logic is in the controller. Business logic is in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.

What this also means is that we can now think about parallel development. For example, one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.

Now the question is - how would they test these components independent of each other? This brings us to another major advantage of adopting this pattern. The MVC pattern makes it easier to test applications. The MVC framework decouples the components and makes heavy use of interfaces, which makes it possible to test individual components in isolation from the rest of the framework.

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/apps/mt-tb.cgi/2001

Comments

Krishna,

When we think about ASP.NET we think this in terms of web forms. With the introduction of MVC, as you explained, there is a clean separation between layers. Now even we have to achieve a Button click functionality, we are not going to get event attached to the button into the form. We would need to capture the event in the Controller and then pass through the Business Logic or Model as per the requirement.

Also the core strength of ASP.NET drag drop server controls is not of any use in MVC implementation.

I don't see much merit of MVC when in comes to rapid development, for which ASP.NET gained the fame.

Hi Krishna,
MVC is not a new pattern ! . MVC was there earlier too.. with 3.5 SP ,They inbuilted a framework for MVC . This made developers work easy to build MVC Project without much head ache. Also you can use pattern in your MVC . MS have a sample project (with depository pattern) http://sutekishop.co.uk/ which is very good to understand !

Thanks for your comments Rajeev and Nanda.

@Rajeev - I couldn't agree with you more. I do feel that ASP.NET MVC might not be a good fit for RAD atleast for the time being. Going forward I am sure that the community as well as MS would come up with more and more complex controls which will make it a little easier to develop richer UI apps. But for the time being we have the power to create our own controls, which is the most time consuming aspect though :). But i would take it as an opportunity to revisit the basic HTML stuff that I guess most of the ASP.NET developers have kind of forgotten :).

@Nanda - Yes, MVC is an UI programming pattern that has been there since ages. But the fact was that there was no support in the .NET framework to enable developers to create MVC based UI applications, which has now been added.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter