Select Right Programming Model with ASP.NET

Decide if ASP.NET Web Forms, ASP.NET MVC, or ASP.NET Web Pages is right for you.

Programming Models

Asp.Net is flexible web platform that enables you to build great web applications using three different programming models. Those models are ASP.NET Web Forms,

All three of these programming models are fully supported options that are used by millions of developers.

Let’s take a Look at how these fit together and why there are three choices.

All these models build on top of the same core ASP.NET web platform.

ASP.NET

ASP.NET and its Programming Models

Each model sits on top of ASP.NET and each model is ASP.NET at the same time, though, each model creates and structures a web application using slightly different approach just like people, each of the approaches different things.

So there is offer for these different approaches because some developers prefer one set of values, and other developers prefer another set of values.

All these programming models are good ones and solve real problems. But the programming model approach that is right for you depends on which feels most natural to you, and which set of values map to values you are looking for.

What Does ASP.NET Web Forms Value?

  • Familiar Control and event-based programming model
  • Controls encapsulate HTML, JS and CSS.
  • Rich UI Controls included – datagrids, chart and AJAX.
  • Browser Differences handled for you.
  • SharePoint builds on Web Forms.

If you have experience of client application development or you are familiar with using UI controls and wiring up event handlers to respond to user actions, you will find a comfortable programming model ASP.NET Web Forms. ASP.NET Web Forms provides a server side web programming framework that enables you to use server side controls with in you HTML Markup. Server Controls in Web Forms encapsulates the complexities of HTML, JAVA Script and CSS. They also support rich features like data binding and Templating.

Web Forms includes dozens of rich user interface controls out of the box. You will be able to retrieve data from databases and wire up controls to display and edit data in datagrids and chart. The controls help you achieve common tasks Differences between browsers are handled by controls.

You may be familiar with SharePoint It builds on top of Web Forms. If you are SharePoint developer, or intend to be, that is another reason for you to use Web Forms.

What Does ASP.NET MVC Value?

  • Feels comfortable for many traditional web developers.
  • Total control of HTML Markup.
  • Supports Unit Testing, TDD and agile methodologies.
  • Encourage more prescriptive applications.
  • Extremely flexible and extensible.

ASP.NET MVC is slightly lower level Web Programming model it does not provide higher level abstractions like controls but rather requires you to know HTML and HTTP a little more deeply.

If you have experience of traditional Web Development background, ASP.NET MVC will probable feel very comfortable to you. ASP.NET MVC gives you total control over the HTML Markup you render and the URLs you expose with in your applications. It enables you easily use the full power and flexibility of CSS and JavaScript. ASP.NET MVC has great support for unit testing and test driven Development of your Web Applications and often used alongside agile methodologies. MVC Encourages strong design patterns and prescriptive applications architectures. It’s very flexible and can be extended in any way you like.

What Does ASP.NET Web Pages Value?

  • Easy to pick up and learn.
  • Inline Scripting Model with Razor and C# or VB.NET.
  • Simplified Model with top to bottom execution.
  • Full control over your HTML.
  • Friendly Helper syntax makes extending your apps easy.

ASP.NET Web Pages is a simple model built around individual pages similar in ways to PHP or classic ASP. It uses the same Razor syntax that MVC Does, but uses inline script rather than separate controller classes. However, you have the full power of the .NET Runtime and all the flexibility of C# and VB.NET at your fingertips. Web Pages gives the developer full control over the generated markup. The reusable methods and libraries that are used in Web Pages are called helpers and they are similar to Web Forms controls in that they encapsulate reusability functionality. However, Web Pages does not contain a page lifecycle like Web Forms does so there is no hierarchy like Page_Load, and Page_Render. You can generally think of an ASP.NET Web Pages as executing top-to-bottom.

Web Pages is also a powerfull tool you can use with in your Web Forms and MVC applications. You can add a Web Page here and there to augment your existing applications or extend them in new ways you perhaps had not thought of.

Remember that choosing your programming model is not a fork in the road that can’t turn back from underneath each of Web Forms, MVC and Web Page is ASP.NET itself. All models share nearly all of their libraries with the core runtime. This include Authentication and Authorization, User Membership, Role Security, Output Caching, Session State, Configuration, Deployment, AJAX and more. The Skills you and APIs you learn in these core libraries can be used interchangeably between Web Forms, MVC and Web Pages. Learn one, and you are well on your way to learning them all. Because it’s all ASP.NET underneath, you can also even choose to have “bybrid” applications that are built with any and all the programming models. It is up to you pick the right tool for the job. As an example, sometime programmers use Web Pages for rapid prototyping and then move to MVC. ASP.NET makes it all possible and gives you a lot of flexibility.

A Quick Intro to MVVM

A quick view to Model–View–X  Paradigm

  • Model-View-Controller (MVC) – 1979
  • Model-View-Presenter (MVP)
  • Presentation Model (PM) by Martin Fowler – 2004
  • Model-View-ViewModel by John Gossman – 2005
  • Prism 1.0 : Composite Application Guidance for WPF – Jul 2008
  • Prism 2.0 : Composite Application Guidance for WPF/Silverlight – Feb 2009

Data Binding in WPF and Silverlight

  • ž     Bind anything to anything
  • ž     Convert types within bindings
  •         Markup based language for expressing bindings

Model-View-Viewmodel pattern

Benefits of MVVM

  • Separation of Concerns
  • —Modularity
  • —Loose Coupling
  • žEmpower Designer
  • žTestability
  • žLess Code
  • žReuse to other Platforms

Shortcomings/Challenges

žNavigation / View Management
—Mediate Pattern – Driver, Controller, Presenter, View Manager
žException Handling
—Where to handle? View or ViewModel?
—Mediator, Events, Interfaces, DataBinding
žPossible Work around:
—Messenger – The Messenger class is a lightweight way of passing messages between various ViewModel objects who do not need to be aware of each other. This is based on the Mediator implementation created by Marlon Grech and Josh Smith, as seen on Marlon’s blog (http://mvvmfoundation.codeplex.com/ )
MVVM – Misconceptions
  • žNo Code-behind
  • žViews have to be DataTemplates
  • žConverters are not needed
  • žViewModel completely wraps Model
  • Only Big projects need to implement MVVM

Summary

žModel-View-ViewModel helps you and your customers to build better apps
  • —Unit testable
  • —Maintainable
  • —Better user experience
  • —Lower costs

 Resources

MVVM
   ○MVVM for WPF by John Gossman http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Prism