Question

I've read a number of good articles about the Model-View-ViewModel pattern and my team intends to implement this pattern in the latest version of our app. I still don't quite get ALL the bits that go together to make this work. I'd like to find a good example of this pattern that I can work through. Something on a small scale, much like Jason Dolinger does in his video here, but I'd like something that I can work through on my own at a slower pace.

Thanks in advance for your help.

Was it helpful?

Solution

Basically a ViewModel is a wrapper around model. Now that might not be very helpful yet :-) Think of the model as the data your application works with, say a person. Now a person has a birthday and you might want to have a form to enter the person's birthday. Suppose we are just using a simple TextBox the date shows up something like 01-02-2009 12:00:00AM. For starters we don't want the time part and we also might not be to happy about the 01-02-2009 part as this depends on your locale settings.

So here the ViewModel comes im. It wraps the Person class and exposed the date as three integer values, year, month and day. In the property set it tries to build a date from the different values and displays any errors that might occur.

So simply said a ViewModel is a Model wrapper specifically geared towards a particular view (display). It eliminates most IValueConvertors at the same time.

Josh Smith has a nice explanation here: http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages and a big discussion here: http://groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1

OTHER TIPS

Prism 4.0 now includes documentation for creating MVVM applications. This is targeted for using the Prism libraries but the concepts work for the MVVM pattern in general.

http://compositewpf.codeplex.com/releases

Here's one more to add to the list: Simple MVVM Toolkit by Tony Sneed (yours truly): http://simplemvvmtoolkit.codeplex.com.

The toolkit consists of helper classes, Visual Studio item templates, and code snippets. In addition to the actual toolkit, there is a sample app with step-by-step instructions on how to build a basic Silverlight app using the MVVM pattern.

Nikhil Kothari has a post about how to use that pattern with his Silverlight.FX library. Check it out here: http://www.nikhilk.net/ViewModel-Pattern-DLR.aspx

I have gone through many recently, the two that stand out are:

Dan Wahlin's (LIDNUG):

http://www.lidnug.org/Archives.aspx

John Papa's (PDC 2010):

http://player.microsoftpdc.com/Session/76864d75-b4da-4858-aac7-786c5f28e344

The best explanation of a ViewModel was given by Scott Hanselman during his MVC talk in Europe.

A one-liner response is -- It is a model (class) that is created for the sole purpose of sending all the necessary data that a particular View needs and also for receiving modified data from the View in case of two-way bindings.

Take a look at my article about real life MVVM in Silverlight. Though it is not for newcomers, and targeting mostly expert developers working on real life projects.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top