Andrew Connell [MVP SharePoint]
1741 Posts |  46 Articles |  5310 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
Andrew Connell's RSS Feed    
AC's Blog Quick Links
SharePoint Quick Links
Article Categories
Archives
May, 2012 (3)
April, 2012 (4)
March, 2012 (4)
February, 2012 (4)
January, 2012 (12)
December, 2011 (6)
November, 2011 (3)
October, 2011 (17)
September, 2011 (8)
August, 2011 (6)
July, 2011 (7)
June, 2011 (13)
May, 2011 (9)
April, 2011 (15)
March, 2011 (1)
February, 2011 (6)
January, 2011 (5)
December, 2010 (11)
November, 2010 (6)
October, 2010 (12)
September, 2010 (5)
August, 2010 (4)
July, 2010 (5)
June, 2010 (6)
May, 2010 (11)
April, 2010 (11)
March, 2010 (9)
February, 2010 (9)
January, 2010 (3)
December, 2009 (10)
November, 2009 (15)
October, 2009 (15)
September, 2009 (7)
August, 2009 (4)
July, 2009 (10)
June, 2009 (8)
May, 2009 (2)
April, 2009 (9)
March, 2009 (6)
February, 2009 (16)
January, 2009 (6)
December, 2008 (12)
November, 2008 (12)
October, 2008 (27)
September, 2008 (13)
August, 2008 (14)
July, 2008 (14)
June, 2008 (12)
May, 2008 (23)
April, 2008 (12)
March, 2008 (15)
February, 2008 (13)
January, 2008 (12)
December, 2007 (10)
November, 2007 (8)
October, 2007 (15)
September, 2007 (20)
August, 2007 (21)
July, 2007 (16)
June, 2007 (8)
May, 2007 (25)
April, 2007 (16)
March, 2007 (18)
February, 2007 (18)
January, 2007 (12)
December, 2006 (16)
November, 2006 (13)
October, 2006 (18)
September, 2006 (22)
August, 2006 (27)
July, 2006 (23)
June, 2006 (23)
May, 2006 (23)
April, 2006 (9)
March, 2006 (17)
February, 2006 (15)
January, 2006 (23)
December, 2005 (31)
November, 2005 (32)
October, 2005 (38)
September, 2005 (53)
August, 2005 (30)
July, 2005 (63)
June, 2005 (30)
May, 2005 (59)
April, 2005 (29)
March, 2005 (74)
February, 2005 (27)
January, 2005 (22)
December, 2004 (32)
November, 2004 (42)
October, 2004 (39)
September, 2004 (20)
August, 2004 (14)
July, 2004 (27)
June, 2004 (40)
May, 2004 (5)
April, 2004 (6)
March, 2004 (16)
February, 2004 (26)
January, 2004 (23)
December, 2003 (7)
November, 2003 (14)
October, 2003 (20)
September, 2003 (4)
Post Categories



Managed Windows Shared Hosting

Over the past year I've spent a lot more time working with Silverlight and WPF (XAML) in building applications and samples for demos or for my own internal utilities. While cutting my teeth and climbing the learning curve, I saw lots of references to a pattern called MVVM or model-view model-morel. The idea behind MVVM is to make life easier in creating custom XAML based applications. Over the last few months I've had a few people ask me how I learned it and why I like MVVM. Like other posts, I like having a post on my blog that I can point people to… so here it goes...

What is MVVM?

There are plenty of people, blog posts and articles that describe it, but here's my take:

There are three pieces in MVVM. The Model is the business component, the backbone of your application. I like to start building this as a class library and then create a separate test project that contains unit tests to test the model. Best part is I can create the entire application ignoring the UI and make sure everything works with unit tests.

The next piece is the View. This is the UI… each view is  a different "screen" or component. There's zero logic here.

The last piece is the View Model. The view model is the glue between the other two pieces. It creates instances of the Model and does all interaction on it. It also has public properties. What happens each view's DataContext is bound to an instance of a view model. Then controls within the XAML of the view have their data sources bound to the properties that exposed by the View Model.

Why does MVVM appeal to me?

The two biggest things that appeal to me with this model is (1) there is a 100% complete separation of the UX and logic of my application. That builds into the second reason which is (2) it's super easy to write unit tests against this type of a solution.

When you leverage WPF's (and Silverlight 4's) commanding framework, it enables you to completely remove the whole event-handler method of building a UX. No more OnClick like events… just bind a button to a command and let the command tell the UX if it is allowed or not at a given time. Best part: wiring up the command (logic) to the UX requires ZERO code… it's all done in markup in the XAML!

How I learned MVVM:

First of all, I had to decide I'd just put my head down and force myself to use MVVM in an application. It took me about 3-5 days to fully understand MVVM as I applied it to a project I was working on.

I first watched a few videos from Pixel8, a podcast by Infragistics. There's one video on doing MVVM in WPF and another with Silverlight that helped with the simplest of examples.

Ongoing, StackOverflow does the best job in finding answers, researching and asking questions around MVVM.

How I do MVVM:

So after learning MVVM and building a few apps, like many other people I've got my own process of creating apps that leverage the MVVM pattern. I thought I'd share these with you:

  • I prefer to build my model first, including all the unit tests. This way I can make sure my application works and thus, when creating the UI I don't have to focus much on the business logic.
  • After the model, I then like to build the view. This allows me to ignore the business logic and focus on just the UX of my app. As I moved through the creation of the different views I can add all the data binding to the controls. Unlike in other technologies, XAML doesn't blow up when the properties I’m binding to aren't present, it just shows up as empty so the view can still work. I can even use the sample data capabilities in Expression Blend from the classes in my model.
  • At this point it's time to wire the model together with the view using a view model. There's one view model for each view. His job is to create instances and interact with the models. It also has the properties that I've binding to from the view. Last step: create an instance of the view model in the constructor of the view and assign it to the data context of the view. What's nice about this is that the view model has zero UX in it so it's fully testable too!
  • I usually build a few views for each component in my app. Sometimes it is iterative… in other words sometimes every time I build the view I build the associated view model.
Technorati Tags: ,,,
posted on Sunday, October 03, 2010 8:54 PM

Feedback

# re: MVVM: Why I like it and how I learned it 10/3/2010 9:09 PM Shyam Narayan
Gravatar I completely agree. MVVM cannot be ignored anymore. Some of the new Visual Studio extensions for SharePoint such as List Definition Designer uses the pattern extensively.

On a lighter note have kept up your New Years Resolution of learning Silverlight ... :)

# re: MVVM: Why I like it and how I learned it 10/3/2010 10:09 PM John Way
Gravatar Great article... and you'll be glad to know that I understood everything you said since we're using it on our latest SilverLight / SharePoint project. It was a bit more of a learning curve for us, but we're sticking with it.
I understand the need for unit testing, but can you recommend any resources to learn how to develop and use them?

Thanks,
John

# re: MVVM: Why I like it and how I learned it 10/3/2010 10:26 PM AC [MVP SharePoint]
Gravatar @John - you mean how to learn/develop unit tests? Not sure where I picked it up, but there are plenty of resources out there. Basically it's a seperate class library (generally) that contains code that calls the public methods of the code you want to test. DRAMATICALLY helps in refactoring and building the model without manually testing... while writing tests slows down development, I think it does speed things up overall as usually only a simple right-click and I've tested my entire app.

# re: MVVM: Why I like it and how I learned it 10/4/2010 7:04 AM Graham
Gravatar Andrew, I agree and thanks for the great article! I am wondering if you have found the best way to use MVVM with SharePoint (Lists, Libraries, Content Types, etc.) in your Model? Are you using SpMetel or are you building your calls to SharePoint's object model in the ViewModel?

Thanks, Graham

# re: MVVM: Why I like it and how I learned it 10/4/2010 8:31 AM AC [MVP SharePoint]
Gravatar @Graham - MVVM doesn't deal with SharePoint directly... it's more of a design pattern when working with XAML based technologies. So it really depends on what you're doing. If I'm building a SL app that pulls data from SP2010, my model is usually either the proxy of the RESTful ListData.svc service or the Client OM. The ViewModel does the work, for the most part, of hydrating and interacting with objects so generally speaking calls like ClientContext.ExecuteQueryAsync() or or working with the CollectionViewSource is handled in the VM.

# re: MVVM: Why I like it and how I learned it 10/7/2010 1:36 PM Anders Rask
Gravatar MSDN magazine also covered Model-View ViewModel in their Fed 09 issue: msdn.microsoft.com/en-us/magazine/dd419663.aspx


# re: MVVM: Why I like it and how I learned it 11/28/2010 2:30 PM Jerry Lanphear
Gravatar AC Said: <<No more OnClick like events… just bind a button to a command and let the command tell the UX if it is allowed or not at a given time.>>

AC,

I am trying to get a handle on how this would work with a very complex page. Let's say we have a page which previously would have been done using the AJAX Toolkit Tab Control and now we are using the Silverlight Tab Control. Let's say that this particular page has 3 main tabs and the first tab has 5 "sub-tabs" in it. This page describes a parent-child items relationship for a particular business entity, let's say a customer and all of its relationships to the business.

So this page has maybe a 100 different states with labels and buttons which need to match the state of each portion of the page. If even one message doesn't match or is left over from a previous action then this is a bug. The user is guided through the page like a "wizard". Butons appear, disappear, enable, disable and even change shape and text based on context.

So the user changes an item on the screen and several things have to change to match the new state of the customer. Where does this logic go? Previously we used events which were responded to in code behind and changed the properties of the controls.



# re: MVVM: Why I like it and how I learned it 11/29/2010 5:43 AM AC [MVP SharePoint]
Gravatar @Jerry - I'm by no means an expert on MVVM. I suggest you post to StackOverflow.com. FWIW, what you describe can be done pretty easily using the MVVM-Light toolkit.

# re: MVVM: Why I like it and how I learned it 11/29/2010 5:58 PM Jerry Lanphear
Gravatar AC,

Actually your article has helped me quite a bit and I think I understand what I need to do now. It really all boils down to the code being testable.

Thanks

# re: MVVM: Why I like it and how I learned it 12/2/2010 9:21 PM Mark Rackley
Gravatar Thanks AC... just what I needed... especially like how you laid out how you tackle MVVM.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 3 and 3 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2012 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License

 
SharePoint Training
Looking for SharePoint 2010 training for developers, administrators, power users, information workers, end users & web designers? Look no further! My company, Critical Path Training offers the best SharePoint training around! We offer public & private classes both as in-person instructor-loed hands-on classes and online classes. Check out our schedule and course catalog for all the ways we can get you going on your SharePoint path!