.tabs-outer { overflow: hidden; position: relative; }

Friday, August 29, 2014

Web Forms vs. MVC

Hello Folks,

In the world of web application development, we can develop web applications using various technologies. ASP.NET is one among many.
Again in ASP.NET, we have two ways to develop web applications, WEB Forms and MVC. These two methods are there on top of Microsoft .NET Framework and have all of its features.
           
Fig 1: Web Application Development

Recently, a lot of discussions are going on whether Microsoft's relatively new Model View Controller (MVC) technology will replace .NET Web Forms as the superior web application development technology. Before deciding which one is better, lets have a glance at following points.

»ASP.NET Web Forms Quick Review                     
  1.  ASP.NET Web Forms for Web Development
  2. ASP.NET Web Forms-Development characteristics
  3. ASP.NET Web Forms-Major disadvantages
»Introduction to MVC
  1. Design Pattern
  2. MVC Control Flow
  3. Driving Goals of MVC
  4. Benefits of MVC
ASP.NET Web Forms Quick Review
  •  ASP.NET Web Forms for Web Development 
    »ASP.NET Web Forms
    • A Visual Studio .NET application type for building web applications
    • One of the three different programming models you can use to create ASP.NET web applications
    • Has been in the market ever since Visual Studio .NET 2002
    • A step further from ASP web applications technology, using the .NET Framework
    • Latest version is ASP.NET 4.5 that uses .NET Framework 4.5
    »Easy-to-use Web Development & Deployment
    • You can use HTML pages, JavaScript, CSS, etc. along with ASP.NET web forms for providing better user experience.
    • Supports configurability through Web.Config file.
  • ASP.NET Web Forms-Development characteristics
    »The ASPX screens
    • Mark-up is like HTML
    • Use ASP.NET server controls primarily
    • Has a code-behind file(.cs file)
    »The ASPX code-behind file
    • This is where you do the server-side logic
    • Handles:
                         »PostBack
                         »Event handling  
                         »Server-side validation
                         »Form-data processing logic
  • ASP.NET Web Forms-Major disadvantages
    1. View states can be large 
    2. PostBack” approach for server-side processing
    3. A big presentation layer code in C# is NOT unit-testable
    4. Presentation layer code that does everything

Introduction to MVC 
A better, more maintainable Development Approach is required for the Presentation Layer, leads to MVC.
  • Design pattern:
    A design pattern describes a proven solution to a recurring design problem, placing particular importance on the context and forces surrounding problem, consequences and the impact of the solution.

    MVC is one of the design patterns. MVC stands for Model-View-Controller.
    »“M” – Models: Tthe source of Application Logic, and Data
    • Contains application business logic (“model services”) and data (“models”)
    • Models” represent entities of business domains, which can be “bound” to a suitable “View” 
    »“V” – Views: For presenting  and accepting user-made changes to data
    • Helps to present data in “Models”
    • You can have multiple “Views” corresponding to the same “Model”      instance
    »“C” – Controllers: Responsible for handling user inputs (incoming HTTP requests)
    • Handles user action
    • In the case of a web app, Controller decides which view to show for incoming HTTP requests from browsers.
  • MVC control flow
    Though MVC comes in different flavors, the control flow generally works as follows:
    1. The user interacts with the user interface in some way (e.g., user presses a button) 
    2. A controller handles the input event from the user interface, often via a registered handler or callback
    3. The controller accesses the model, possibly updating it in a way appropriate to the user’s Action. Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension
    4. A view uses the model to generate an appropriate user interface. The view gets its own data from the model. The model has no direct knowledge of the view (However, the observer pattern can be used to allow the model to indirectly notify interested parties, potentially including views, of a change)
    5. The user interface waits for further user interactions, which begins the new cycle

    Fig 2:MVC Control Flow summarizes the relationship between the Model, View, and Controller is provided below.


    Fig 2:MVC Control Flow
  • Driving goals of MVC
    1. Clean separation of concerns
    2. Highly extensible and pluggable architecture
    3. Powerful URL routing component
    4. Includes existing .NET features Master pages(only in MVC2, later versions have layout), content pages, etc.
    5. Full control of HTML markup
    6. Since MVC3 Razor view engine has been introduced.
  • Benefits of MVC
    1. Testability
    2. Flexibility
    3. Extensibility
    4. Perfect for team work
    5. Powerful Routing System
    6. Ready-made ASP.NET platform features
    7. ASP.NET MVC meshes so well with the popular jQuery library
    8. MVC Framework crafts a simple, elegant markup styled with CSS

Conclusion
Web Forms vs. MVC, which one is better?
There is no straightforward answer, and rightly so; they both provide different means to reach the same end. Even Scott Guthrie of the MVC development team stated that, "Different customers look for different programming approaches, and a lot love Web Forms and think it is great. Others love MVC and think it is great. That is why we are investing in both". 
 
In terms of development speed, which would be faster: webforms or mvc?
This depends on many factors, but suppose that you use equal technologies both with Webforms and MVC then MVC should be faster. Web forms has some overhead because of the mismatch between how things really work (eg. that http is stateless) and what is assumed while writing the code (you're programming as if the application was stateful).

On long term basis, which would be easier to maintain?
In my opinion, MVC is easier to maintain, because it's easier to change bare html/js code, the model(MVC) is well defined which makes it, less painful for a new person to figure out how everything works.

Hope this helps, Happy Coding.!!
Please do like, If you find this post insightful.

Thanks,
Ram 

No comments:

Post a Comment