Search This Blog

Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, March 5, 2012

Model View Controller



Model View Controller (MVC) is a standard design pattern practiced by many developers. Web applications will benefit from the Model View Controller (MVC) framework. But still developers continue to use the traditional ASP.NET application pattern that is based on Web Forms and postbacks. Some developers combine the two approaches as neither approach excludes the other types of Web applications.
Model View Controller (MVC) pattern creates applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The Model View Controller (MVC) framework includes the following components:

Model

Model objects are the parts of the application that implement the logic for the application’s data domain. Model objects sometimes retrieve and store model state in a database.
For example:
  • If a Product object retrieves information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database.
In small applications, the model is often a conceptual separation instead of a physical one.
For example:
  • If the application only reads a dataset and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the dataset takes on the role of a model object.

View

Views are the components that display the application’s User Interface (UI). The User Interface (UI) is created from the model data.
For example:
  • 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.

Controller

Controllers are the components that handle user interaction, work with the model. Controllers then select a view to render that displays User Interface (UI). In an Model View Controller (MVC) application, the view only displays information. The controller is responsible for user input and interaction.
For example:
  • The controller handles query-string values and passes these values to the model. These models then uses these values to query the database.

Model View Controller

As mentioned before, Model View Controller (MVC) pattern specifies where each kind of logic should be found in the application. The User Interface (UI) logic belongs in the View. Input logic belongs in theController. Business logic belongs in the Model.
This pattern will help you manage complexity in building applications, as it enables you to focus on one aspect of the implementation at a time like for instance, you can focus on the view without depending on the business logic.
The loose coupling between the three main components of an Model View Controller (MVC) application also allows parallel development.
For example:
  • A 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.

Creating MVC Applications



Careful consideration should be taken when building a Web application using either the ASP.NET MVC framework or the ASP.NET Web Forms model. MVC framework does not replace the Web Forms model therefore you can use either framework to build Web applications.

The following is a comparison of the advantages of both MVC framework and the Web Forms model use when building a Web site.

Advantages of MVC approach

ASP.NET MVC based framework advantages are as follows -
  • MVC approach makes it easier to manage complexity by dividing an application into the Model, the View, and the Controller.
  • MVC approach does not use view state or server-based forms. Thus making the MVC framework ideal for developers who require more control over the behavior of an application.
  • MVC approach uses a Front Controller pattern. The  Front Controller pattern processes Web application requests through a single controller thus enabling you to design applications supporting rich routing infrastructure.
  • MVC approach provides better support for test-driven development (TDD).
  • MVC approach works well for Web applications supported by large teams of developers. This approach also supports Web designers who require a high degree of control over the application behavior.

Advantages of Web Forms approach

Web Forms based framework advantages are as follows -
  • Web Forms approach supports event model that preserves state over HTTP. This benefits Web application development like Line-of-Business. Web Forms-based application provides many events supported in hundreds of server controls.
  • Web Forms approach uses a Page Controller pattern. This Page Controller pattern adds functionality to individual pages.
  • Web Forms approach uses view state on server-based forms thus making state information management easier.
  • Web Forms approach works well for small teams of Web developers and designers. This makes sure that a large number of components are available for rapid application development.
  • When compared with MVC approach, Web Forms approach is less complex for application development. The components  that is the Page class, controls, etc, are integrated closely and usually require less code than the MVC model.