views:

73

answers:

4

I've been reading up on MVVM and so far have found it very interesting. Most of the examples I've found, however, are for Windows apps, as opposed to Web apps. I've also seen a lot of mention of MVVM used with Silverlight, and I know Silverlight can be used for either Web or Windows apps.

So my question is - is MVVM a valid pattern for Web-based apps? If it is, does the UI have to be Silverlight? I'm in the process of deciding which technologies to use for a new mid-size website we need to design, and Silverlight may be a hard sell to the powers-that-be, though what we use behind the scenes doesn't matter so much.

Any info anyone can supply on using MVVM in a web environment would be appreciated. Example code would be great as well.

+1  A: 

MVVM is totally acceptable for Web development. In fact, it is recommended for Silverlight development. Our company uses MVVM + Silverlight for many of our projects with great success. The initial learning curve can be tough, but once it clicks, it offers a lot of benefits.

In my opinion, to make MVVM really work, you need a framework that has right binding support. Otherwise, you'll have to write a lot of "glue" code to join your view and view model. Silverlight has excellent binding support and if done correctly, you can eliminate most of the code-behind in your view so all of your business logic stays right in your ViewModel.

Tim Heuer has some excellent tutorials and videos on MVVM with Silverlight. I highly recommend going through his stuff. http://timheuer.com/blog/articles/getting-started-with-silverlight-development.aspx

j0rd4n
A: 

MVVM is essentially the MVC pattern with specific changes to support development of applications using Windows Presentation Foundation.

Model - View - ViewModel
Model - View - Controller

So the ViewModel is the Controller in MVVM. The pattern is very good; it makes it very easy to construct applications that are simple yet powerful, and that are easy to test and maintain.

If you're looking to use MVVM in a web application that is NOT Silverlight, look into ASP.NET MVC. MVVM is also an option if you are using Silverlight. You can even mix the two, hosting your Silverlight app in a MVC website.

Will
+1  A: 

For web development I would rather go for MVC. If its purely Silveright then MVVM can be considered

ajay_whiz
I agree. I've read an article about the pitfalls/design issues a guy had while implementing MvvMw in Silverlight; it was quit a lot. Compare that to the difficulties with asp.net mvc (none, more or less) i would definately use MVC over MvvM.
Michel
as far as i understood, MvvM is in Silverlight used with automatic binding between the data and the view: when the data (model) updates, the view gets notified and displays the updates. I'm not sure if this is the ONLY implementation for MvvM, but the automatic updating model from silverlight is something i don't use in webprojects.
Michel
+1  A: 

For web web(html) it's not really usable since the point mvvm is to have an interface reflect changes in the viewmodel immediately. (through databinding/events).

For web, a change in the viewmodel is usually a post + complete rebuild of the screen.
So why bother..

However if you have an AJAX website with one fixed HTML page if which the content is continually updated with javascript. Then it becomes interesting.

Julian de Wit
Exactly. The entire point of MVVM is to take advantage of data-binding. Otherwise, the existing patterns that have been around for ages work fine. The ViewModel is special in that it encapsulates view logic and exposes properties and actions to the view through data-binding. This works great in a stateful application, but the web is, by nature, stateless, so the pattern breaks down there. MVC is much better for non-Silvelright web apps IMHO.
Jeremy Likness