Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

One challenge with Silverlight controls is that when properties are bound to code, they're no longer really editable in Blend. For example, if you've got a ListView that's populated from a data feed, there are no elements visible when you edit the control in Blend.

I've heard that the MVVM pattern, originated by the WPF development community, can also help with keeping Silverlight controls "blendable". I'm still wrapping my head around it, but here are some explanations:

  • http://www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx
  • http://mark-dot-net.blogspot.com/2008/11/model-view-view-model-mvvm-in.html
  • http://www.ryankeeter.com/silverlight/silverlight-mvvm-pt-1-hello-world-style/
  • http://jonas.follesoe.no/YouCardRevisitedImplementingTheViewModelPattern.aspx

One potential downside is that the pattern requires additional classes, although not necessarily more code (as shown by the second link above). Thoughts?

like image 994
Jon Galloway Avatar asked Dec 17 '08 17:12

Jon Galloway


People also ask

Why do we need model-view-ViewModel pattern MVVM pattern?

Model and ViewModel work together to get and save the data. View: The purpose of this layer is to inform the ViewModel about the user's action. This layer observes the ViewModel and does not contain any kind of application logic. ViewModel: It exposes those data streams which are relevant to the View.

What is the use of ViewModel in MVVM?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

What are the disadvantages of MVVM?

Disadvantages of MVVM Model:This can confuse the developer and make the development or debugging process complicated. When it comes to an Android environment, the user is restricted with only two ways to work with View. Moreover, they can either use Data Binding or any other View method.

Is MVVM an overkill?

MVVM is Overkill But it does work sometimes. In really simple CRUD applications, it works great.


1 Answers

I definitely think you should use the MVVM pattern for Silverlight applications - and one of the benefits of the pattern is that you can actually make your application really blendable through some simple techniques. I often refer to "blendability" as "design for designability" - that you use certain techniques to make sure your application looks great in Blend.

One of the techniques - like Torbjørn points out - is to use a dependency injection framework and supply different implementations of your external services depending on wether the code is being executed in Blend or in the Browser. So I configure my container to use a dummy data provider when the code is executing in Blend, and that way you get design time support for your list boxes, data grids etc.

The challenge is often how to set the DataContext declaratively - so I often end up using a service locator class a a "front end" to the IoC container. That way I can bind the data context to a property on the service locator.

Another technique is create some kind of ObjectDataSource control (non visual) that has two properties: Design Time DataContext and RunTime Data Context. The control does the job of detecting where is being executing, and then setting the Parent DataContext to the right object.

like image 154
Jonas Follesø Avatar answered Sep 19 '22 16:09

Jonas Follesø