Tuesday, November 23, 2010

Introduction To MVC

ASP.NET MVC Overview

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. 

The MVC pattern helps you create 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 UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs 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. 

ASP.net MVC is an alternative approach to web forms rather than a replacement. It will not replace web forms and web forms will not replace ASP.NET MVC. The fact is that ASP.NET MVC and web forms will co-exist and that ASP.NET MVC is not a replacement for web forms.

Problems with ASP.net Web Form
·         Web forms introduced event-driven approach and also introduced Viewstate and Postback. The end result is that web forms break the stateless nature of the Web.
·         Both Viewstate and Postbacks have been made lot of problems and increased complexity of the web application development.
·         Many web pages having hundreds of KB size of Viewstate that affected the performance of the applications sometime.
·         Developers do not have the control of the rendering HTML of web forms and Server controls that render html with mixed inline style and deprecated tags that does not follows standards.
·         Another problem with Web Forms is the integration of JavaScript frameworks due to the naming conventions of rendered HTML.
·         The page life cycle of the Web Form is too complex and has the tightly coupling between all things in the ASP.net framework and a single class is used both to display output and handles user input.
·         So unit testing is almost an impossible task. Since web is a stateless thing, Events, Postbacks and Viewstate are not a good way.
·         Many ASP.net web form developers are facing different type pf browser compatibility issues when developing public face internet applications.
·         It uses a Page Controller pattern that adds functionality to individual pages.

ASP.NET MVC Approach
·         The ASP.NET MVC simplifies the complex parts of ASP.net Web Forms without any compromise of the power and flexibility of ASP.NET platform.
·         ASP.net MVC implements Model-View-Controller UI pattern for web application development that lets you allows to develop applications in a loosely couples manner.
·         MVC pattern is separating the application in three parts- Model, View and Controller.
·         A view is responsible for rendering the user interface (UI) of the application and it is nothing more than html templates that filled with application’s data passed by the controller.
·         The Model implements the logic for the application's data and it represents the business objects of the application that using the View for rendering user interface.
·         Controllers are handles and responds to user input and interaction. The web request will be handled by the controller, and the controller will decide which model objects to use and which view objects to render. The MVC model replaces the Web Form events with the controller actions.
·         The main advantages of the MVC models are clear separation of concerns, unit testing facility, and more control over the URLs and HTML.
·         The MVC model does not use Viewstate, Postbacks, Server controls, and server-based forms that enable full control over the application and html rendered by the Views.
·         MVC model is using Representational state transfer (REST) based URLs instead of file-name extensions used by the Web Form model so that we can make search engine optimization (SEO) URLs published by the application.
·         It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure.

Choose Best Approach:
·         If you want more control over the HTML or you want Test Driven Development (TDD), or you care about web standards, accessibility, or you want to build SEO based URLs, you can choose MVC model.
·         If you want rich controls and state oriented event-driven web development, you can choose Web Forms model.

MVC Features
·         You can easily integrate with jQuery and other JavaScript frameworks with MVC. Using extension methods of C# 3.0, we can make powerful and rich HTML helper methods.
·         The MVC model allows building highly testable, maintainable loosely coupled applications with good practices such as TDD, Separation of Concerns (SoC) and Dependency Injection (DI).