Introduction to MVC
Model-View-Controller (MVC) is an architectural pattern. This pattern separates an application into three components Model, View and Controller. The MVC framework is a lightweight and highly testable presentation layer that is integrated in web form applications.
MVC Architecture
In a traditional web forms application:
- The Model is the part that responsible for handling the business and data, such as retrieve data from the database or from other storage or perform business rules on data. It is independent of the View and Controller.
- The View is the part that displays the user interface. Ideally it shows model data. In other words, we can say that the view represents the model data given by the controller.
- The Controller is the part responsible for handling user requests. The Controller interacts with both the Model as well as the View. The Controller interacts with the model, gets the data and this data is passed to the view.
Problem with ASP.Net Web form
Web form applications introduced the event-driven approach and also introduced View State and post-back. Both post-back and view state create problems, in other words view state affects the performance of the application (sometimes the web page has hundreds of KB of View state). The developer does not have any control of the rendering of the server control. Also the Page lifecycle of an ASP web form is too complex. In a web form application, a single class displays the user interface and handles the user controls.
There is no predefined project structure like MVC for creating a web form based application. One may use or follow their own style of coding and practice. Also Code behind is tightly coupled with the UI.
The Separation of Concern has not been fit with web form based applications. Also automated testing is very difficult in a post-back model.
Advantages
The following are the advantages of MVC:
- MVC does not support view state and server based forms this gives full control on developer's hand to control application behavior.
- MVC contain three main parts model, view and controller that help to manage complexity of application.
- MVC internally use Front Controller pattern that resolved decentralization problem exists in page controller by passing all request through single controller and this controller itself divide into to two part handler and hierarchy of commands. Front controller pattern has also have its own benefit such as Centralized control, thread safety and configurability.
- MVC provides better support to TDD (Test driven development). TDD is related to the test first programming concepts of extreme programming. It helps us to reduced time in reworks and helps create loosely coupled code.
- MVC enforces separation that reduces complexity of project structure.
- In MVC, layers are loosely coupled and architecture enforces separation so it supports parallel development.
- MVC has builtin support rich routing features. It enables us to use URL that does not need to map specific page or file in web site.
- MVC support SEO and REST friendly URL.
- MVC is easily integrated with JavaScript framework.
- MVC is also support third-party view engine.
Disadvantages
Even if the MVC pattern comes with many advantages, there are also some disadvantages too.
- ASP.Net MVC sites require more time to develop over traditional ASP.Net applications.
- There is no builtin rich UI control.
- In MVC, there is extensive use of jQuery / JavaScript / AJAX, it makes the form (application) complicated.
- ASP.NET MVC is difficult to learn, it requires a deep knowledge of the pattern.
- View state and post-back will not work as designed so that we cannot use a control such as Grid view, repeater and datalist control.
Advantages of web form based application
The following are the advantages of web form based applications:
- Web forms use the Page Controller pattern. In the Page Controller pattern, input is accepted from the page request, invokes the requested action on the model (business layer) and determines the corresponding view (page). In other words, in web form based applications, when the request is served by a specific ASPX page. Hence the page itself acts as a controller.
- Web forms based applications support the event driven programming model that preserve the state over HTTP. There are many events and controls supported by web form based applications. Web forms applications have a large tool box. We can also integrate third-party controls.
- It provides the Rapid Application Development (RAD) programming model.
- View state is used on Server-based that make easier state management.
- In web forms, components are tightly coupled and require less code than the MVC.
MVC based application Visual Studio web form based application
MVC based application |
Web form based application |
It supports the Test Driven Programming model |
It supports the Event Driven Programming model that preserves state over the HTTP. |
There is no event in MVC. |
The Web Forms-based application provides many events and also is supported in many server controls. |
It uses the Front Controller pattern that process all requests through a single controller. |
It uses the Page Controller pattern that adds functionality to individual pages. |
MVC does not support view state. |
It supports view state on server based forms that can help us to manage state information. |
MVC has no specific page life cycle. All individual components (such as Model, View and Controller) have their own life cycle. |
There is a specific page life cycle in the web form page. |
MVC supports Separation of Concerns (SoC) that provides the highly organized code structure. In MVC all three parts can be developed and worked independently. |
Web forms support Rapid Application Development (RAD). The components are tightly integrated. |
MVC is an extensible and pluggable framework. We can create our own view engine to render the view, we can also plug in a URL routing policy and other components. MVC also supports DI and IOC. MVC also supports multiple View Engines such as ASPX ,RAZOR, and so on. |
A Web form does not support customized view engine. |
MVC follows the design of the stateless nature of the web. |
Whereas web forms preserve state over HTTP. |
Code behind is not tightly coupled with the View (User Interface). |
Code behind is tightly coupled with the View (User Interface). |
MVC also supports existing markup such as ASP.Net page (.aspx), user control (.ascx) and master page (.master) as view templates. |
MVC also supports existing ASP.NET features such as authentication (form and windows), authorization, membership and roles, caching, session state and profile state management. |
There is a loose coupling among the three components of an MVC application that also supports parallel development. |
The ASP.NET MVC framework is a lightweight alternative of web form based applications. |
Conclusion
The selection of technology varies from person to person. MVC has a few advantages, such as more control over HTML; support of TDD, builtin support of SEO based URLs, and so on. If you want these benefits then you can choose the MVC model. A Web form's abstraction of the Web provides a stateful environment, while MVC is consistent with the Stateless nature of the web. I think, testability, refactoring capability and maintainability are the main factors of any application. The MVC model allows us to create highly testable and loosely coupled applications with Test Driven Development (TDD) and Separation of Concerns (SoC). Now the complete decision of the team's and project's requirements to choose technology between web forms or MVC.