Should I use the Model-View-ViewModel (MVVM) template in Silverlight projects?

One problem with Silverlight controls is that when properties are bound to code, they are no longer editable in Blend. For example, if you have a ListView that is populated from a data feed, the controls are not visible when editing the control in Blend.

I heard that the MVVM template created by the WPF development community can also help keep Silverlight controls “blendable”. I am still hugging him, but here are a few explanations:

One of the potential drawbacks is that the template requires additional classes, although not necessarily more code (as shown in the link above). Thoughts?

+41
design-patterns silverlight mvvm
Dec 17 '08 at 17:17
source share
12 answers

I definitely think you should use the MVVM template for Silverlight applications, and one of the advantages of the template is that you can really make your application really mixed with simple methods. I often refer to “compatibility” as “design for convenience” - you use certain methods to make sure your application looks great in Blend.

One of the methods, as Thorbjørn points out, is to use the dependency injection platform and provide various implementations of your external services depending on whether the code is executed in Blend or in the browser. Therefore, I configure my container to use a dummy data provider when the code runs in Blend, and this way you get development-time support for your lists, data grid, etc.

The problem is how to declare the DataContext declaratively - so I often end up using the front-end services locator class a in the IoC container. That way I can bind the data context to a property in the service locator.

Another method is to create some kind of ObjectDataSource control (not visual) that has two properties: Design Time DataContext and RunTime data context. The control performs the task of determining the location of execution, and then sets the parent DataContext to the correct object.

+32
Dec 19 '08 at 9:21
source share

I am not sure if I can answer your question, but I think the article below is very valuable. Jonas Follesø uses ninject to switch his services in design / blend mode. Very nice!

http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx

+4
Dec 17 '08 at 17:25
source share

I also agree with Jonas regarding MVVM with Silverlight. I really think that MVP is also a good choice, but I recently managed to try MVP and MVVM with Silverlight, and I am much happier with the results of MVVM. (Yes, I changed my mind, the more I used MVVM). The VM abstracts the model binding from the view (obviously) in MVVM, which allows the use of scripts with more binding (at least cleaner ways to execute them) than with MVP. This is only one aspect.

I will post some examples of MVP and MVVM with Silverlight on my site.

+3
Jan 08 '09 at 4:35
source share

I tried several options and I rely on MVVM as the best choice for me. Miscibility is an important point, and I also find the virtual aspect intuitive to fine-tune dynamic behavior and procedural effects and animations (e.g. Nikhil Silverlight.FX). At one point, I tried to avoid Blend altogether, thanks to the free interfaces, but I find that the connection between the user interface and the behavior is too painful in the long run. I want to create my own interface in Blend and then add effects and other behavior in the code, this proves that this is the best sample for me to follow so far.

+2
Dec 17 '08 at 20:13
source share

I think many of us are waiting for the pioneers to go ahead and create really good sample applications using MVVM in Silverlight (and WPF, for that matter). There are several difficult areas, such as the lack of ICommand in Silverlight or the difficulty of interacting with animations , starting and stopping only using data binding.

This is definitely a model to keep an eye on the future, and it is worth a try if you don't mind "cheating" from time to time in places where you cannot understand it.

+2
Dec 24 '08 at 23:18
source share

I agree with Jonas . MVVM seems like the model that works best for me (although John Dad thinks MVP makes more sense). I have an MSDN article about this in March, which I hope will answer the call for a good example.

By the way, I would like to see some cohesion in the MVVM Framework department. There is no good solution for the framework to be followed. I like Jonas (I think Jonas is the FX Framework), but since its WPF compatibility might not be the right choice for some.

+2
Dec 27 '08 at 13:07
source share

I like the ViewModel template and highly recommend it. There are several types of "Getting started with ViewModel" posts on my blog.

+2
Jan 10 '09 at 5:51
source share

Recently, I have been using MVVM in several Silverlight projects, and it works very well, I would definitely recommend it. The Jonas post is a great place to start, I recently blogged on my MVVM too, and created a really simple solution to demonstrate the main points of contact.

+1
Dec 20 '08 at 13:55
source share

There is a very good introduction to Techdays 2010 in the MVVM template, clearly explained:

For more complex applications that require a higher degree of automated testing, this definitely makes sense, and moving from DependencyProperties dependencies to DataContext bindings is much neater than the ASP.NET counterpart.

The biggest problem I encountered in Silverlight is checking the actual interface (where I think), and the huge confusion of event calls that you get when using WCF services (or WebClient, for that matter) with Silverlight.

+1
Apr 28 '10 at 13:23
source share

I always thought that MVVM and PresntationModel http://martinfowler.com/eaaDev/PresentationModel.html are essentially the same thing. PresentationModel is a lot easier said. I have used it successfully in java swing, window forms, WPF and Silverlight. If you think in terms of separation of concerns, the presentation model makes a lot of sense. You have one class whose only problem is to provide a presentation-friendly model. It really doesn't matter what technology is used to display on the screen. This may change some implementation details, but separating issues separately is a good idea no matter how you display the information. Because of this separation, you can easily write tests against your presentation model regardless of viewing technology. So a plus.

0
Dec 20 '08 at 14:13
source share

With the release of Prism v2 in February 2009, P&P even better MVVM support is now available for Silverlight and WPF. See microsoft.com/compositewpf for details.

0
Feb 22 '09 at 23:52
source share

Take a look at my article on MVVM and Silverlight in real projects and decide for yourself.

http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

0
Mar 05 '11 at 19:05
source share



All Articles