Click here to Skip to main content
15,885,904 members
Articles / Web Development / ASP.NET

Why(s) & How(s) of Asp.Net MVC Part 1

Rate me:
Please Sign up or sign in to vote.
4.88/5 (70 votes)
1 Mar 2013CPOL10 min read 154.3K   174  
Asp.net MVC fundamental ideas, advantages and details about request processing.
This is an old version of the currently published article.

Background

MVC is the new buzz in development word, everyone is talking and developing with mvc. Knowing how to use Asp.Net MVC is easy, google can give you hundredes of good resources thats shows how to configure and write codes. But from my point of view its always better to start from asking why before asking how. Moreover MVC is not alone it wraps around lots of other concepts like db first model first, code first, razor, TDD and so on and on.
Image 1
Each concept itself is a giant and if you google, will find lots of good resources on them. Starting from this post I am planning a series of articles explaining each of the above concepts or more and slowly move forward to implementation applying these concepts explaining benefits/drawbacks of using those. So plan is to take small steps to reach to the ends. In this first post will discuss benefits, applicability of MVC and life cycle of Asp.Net MVC.  

Why MVC

"Splits user interface interaction into three distinct roles. - Martin Fowler" was the core motivation of MVC. Initially MVC was born as the outcome of the battle of application logic vs user interface. Now abovious question is whats is this all about? Idea is to keep a good separation between the presentation of a program (which is UI) and the rest of the functionality. A separation lets you address on each aspect of the layer separately—and one complicated thing at a time. It also lets specialized people to work on the separate pieces codes. In effort to make both of ditinguised an approach/pattern was needed and this approach is the most highlighted part of Model View Controller (MVC). Separation of view from rest of the application logic open a door to change the view logic in future. May be afterwards client asking for more extractive UI or more UX support or worse a complete makeover. If view logic is tighly coupled with business logics it will be difficult to made the change.
Image 2
The MVC pattern was first implemented as part of the Smalltalk class library. It was originally used as an architectural pattern for creating graphical user interfaces (GUIs). Later when adopted in the web aplication the meaning and applicability changed radically. The motivation behind MVC is reusability and separation of concern. All other benefits is stand on this two building blocks.

Asp.NET MVC

The ASP.NET MVC Framework is Microsoft's effort to create an ASP.NET programming environment wraped around the MVC pattern. Like the official site says "ASP.NET MVC is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework.", Asp.net MVC is sitting on top of .NET framework and utilizes the core functionality of asp.net. And it definatly does not mean web forms are now extinct or too old to use. In my opinion Asp.net MVC is a better way to doing stuffs that we are used to do with web forms.
Image 3
Now the question is how difficult is to adopt this new framework for developers who are used to with RAD(Rapid Application Development) style? and the answer is its very easy. How? though we are using MVC we are still using visual studio and we are still utilizing asp.net framework, isn't that cool? Yes server controls are not available , but think about how clean the markup would be if there is no server control and as the model/view are all separate testing is lot easier. In the context of our very own ASP.NET web applications, the model, view, and controller participants can be explained as:

  • View: This is the HTML markup in ASPX pages. This is rendered in the presentation tier (the browser).
  • Controller: This refers to a simple class controller that decide which model needs to be shown respect to which view.
  • Model: This a layer who deals with data, which may be processed by the business layer.
Page controller pattern, which is the default architecture in the ASP.NET web forms has some limitations.Each page in application has a separate page controller so there could potentially be a lot of code in the code-behind files and lots of maintenance effort. Second issue is testing the application, especially the GUI and the code-behind classes is very difficult, and we cant use test automation tools. Unlike web form which use Page controller pattern, MVC is based on a front controller design, where a centralized controller decides which view to render. And as model, view and controller are all separate so automated testing has never been easier as today with Asp.Net MVC.
Image 4  

Benifits

Before we come to conclusion whether to use MVC or not lets look couple of benefits of using MVC:

  1. Distribute development effort to some extent, so that implementation changes in one part of the Web application do not require changes to another. The engineers responsible for business logic and engineers responsible for flow of control, and Web-page designers can work independently.
  2. Easily prototyping is possible. You might do as follows, for example: Create a prototype Web application that accesses several workstation-based programs. Change the application in response to user feedback. Implement the production-level programs on the same or other platforms.
  3. You can more easily migrate legacy programs, because the view is separate from the model and the control and can be tailored to platform and user category.
  4. You can maintain an environment that comprises different technologies across different locations.
  5. The MVC design has an organizational structure that better supports scalability (building bigger applications) and ease of modification and maintenance (due to the cleaner separation of tasks)
  6. It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.
  7. It provides better support for test-driven development (TDD).
  8. It uses a Front Controller pattern that processes Web application requests through a single controller. Thus can design custom routing infrastructure
And if we consider web forms as an alternate, lets see what web forms has to offer,
  1. It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.
  2. It uses a Page Controller pattern that adds functionality to individual pages. For more information, see Page Controller.
  3. It uses view state on server-based forms, which can make managing state information easier.
  4. It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.
  5. In general, it is less complex for application development, because the components are tightly integrated and if we consider LOC then require less code than the MVC.

Image 5  

Asp.net MVC Lifecycle

To Understand the MVC request execution Process, we have to start from the very beginning which is IIS. Lets look at it from an IIS perspective now. As Asp.NET MVC is sitting on top of asp.net framework, so from the user request intercept by HTTP.sys to worker process processes the request stays the same as as asp.net web application. Though ASP.NET is executed in different ways in different versions of IIS, but thats discussion is out of context for now. We will concentrate the part where the HTTP request enters the the MVC Framework.
Image 6
Asp.Net request processing can be described in 4 simple steps:

  1. Step 1 – Routing : This first step once an ASP.NET application first starts.The UrlRoutingModule intercepts every request. To let MVC routing handler to process requests, configure the routing table during application start-up is required. While configuring MVC with default template, a default routing configuration is provided in global.asax file.
  2. Step 2 – MvcHandler : The MvcHandler creates a controller, passes the controller a ControllerContext, and executes the controller.
  3. Step 3 – Controller : The MVC controller factory locates and creates the controller in CreateController. The controller determines which controller method to execute, builds a list of parameters, and executes the method. The ControllerContext is passed to the Execute() method on the controller class.
  4. Step 4 – Action : Controller methods end with a call to either the RenderView() or RedirectToAction() method. The RenderView() method is responsible for rendering a view (a page) to the browser. The Controller.RenderView() method delegates its work to a particular ViewEngine.
In Short the steps are,
Request -->Routing --> MvcHandler -->Controller --> Action
Image 7
The process described above is default behavior, but asp.net mvc allow fine tune the process according to need, even you can improve performance by using specialized controller like asynchronous controller. As of now we covered with basics of asp.net mvc, hope I didn't missed out any basic concepts. In next post we will take a deep dive into Asp.Net MVC describing how to utilize mvc built-in features comparing to web forms.
One important tip is when an ASP.NET MVC Web application runs in IIS 7.0, no file name extension is needed but in IIS 6.0 you should map the .mvc file name extension to the ASP.NET ISAPI DLL.

References and Good Reads

Series Plan

  1. Why(s) & How(s) of Asp.Net MVC Part 1 - MVC Basics
  2. Why(s) & How(s) of Asp.Net MVC Part 2 - Playing with Asp.Net MVC
  3. Why(s) & How(s) of Asp.Net MVC Part 3 - DB First
  4. Why(s) & How(s) of Asp.Net MVC Part 4 - Model First
  5. Why(s) & How(s) of Asp.Net MVC Part 5 - Code First

History

Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.