Friday, September 03, 2010    
Blog  
OpenLight Blog
Apr 7

Written by: Michael Washington
4/7/2010 8:07 PM 

Why Use View Model Style?

The biggest benefit is that it allows a developer to create an application with no User Interface (UI). A Designer is then able to create the entire UI using Microsoft Expression Blend 4+, without writing a single line of code.

The benefits of this include:

  • The UI is easier to create when you can do it visually, in a tool, without having to write code
  • You may have a better UI when it is designed and implemented by people who may not be programmers
  • The UI can be created first, or at the same time, as the rest of the code, if needed
  • The UI can be completely changed without the need to change any code

To achieve this, all you need to do is create a UI (a View) that has no code behind, and communicates with the application using bindings and commands that are contained in a View Model.

 

The Model

image

The Model is where the data for the application goes, The Model can contain:

  • Web Services – A Silverlight application typically needs to communicate with the web server to get the data, you can put your web service methods here.
  • Rest Services – The same as web services.
  • Generic Collections – Basically any data.

 

The View Model

image

The View Model consists of:

  • Properties – One of something. This could be a String or an Object. Implements INotifyPropertyChanged, so that any element bound to it, is automatically notified whenever it changes.
  • Collections – A collection of something. This is of type ObservableCollection, so that any element bound to it, is automatically notified whenever it changes.
  • Commands – An event that can be raised. Also, one parameter of type Object can be passed. This implements ICommand.

 

The View

image

This is the part that you can make with no code using Expression Blend.

An Outline

  • Model
    • Get your data any way you can. Usually calls to web services.
  • View Model
    • Consists of:
      • Properties – One of something. This could be a String or an Object. Implements INotifyPropertyChanged, so that any element bound to it, is automatically notified whenever it changes.
      • Collections – A collection of something. This is of type ObservableCollection, so that any element bound to it, is automatically notified whenever it changes.
      • Commands – An event that can be raised. Also, one parameter of type Object can be passed. This implements ICommand. 
        • Implemented using Behaviors. Mostly the InvokeCommandAction Behavior  
  • View
    • Properties
      • Bind to a text box, radio button, toggle button, MediaElement, trigger an animation or ViewState change
    • Collections
      • Bind to List box, TreeMenu
    • Commands
      • Implemented using InvokeCommandAction behavior
        • Bind to a ICommand in the ViewModel
        • Indicate the ICommand that you want to raise
        • Pass a parameter

To Learn View Model, Start Here: [RIATasks: A Simple Silverlight CRUD Example]

Tags:

5 comment(s) so far...

Re: Silverlight View Model Style: An (Overly) Simplified Explanation

Very nicely explained. Concise and clear, thanks.

By Mike on   8/22/2010 4:11 PM

Re: Silverlight View Model Style: An (Overly) Simplified Explanation

Great post Michael. One thing that is unclear to me is that if the only goal of MVVM is to allow the UI to be developed independently of the rest of the application, why does the pattern describe a "View Model" approach when that would not impact the separation of the UI?

By Dave Bartlett on   8/26/2010 3:06 PM

Re: Silverlight View Model Style: An (Overly) Simplified Explanation

@Dave Bartlett - "View Model Style" is basically MVVM in regards to the separation of the View and the "View Model". The only reason for a different term is to avoid the arguments that arise when you use MVVM, but do not adhere to all of it's "generally understood" "rules".

"View Model Style" has no rules :)

By Michael Washington on   8/26/2010 3:11 PM

Re: Silverlight View Model Style: An (Overly) Simplified Explanation

great post...as the title says the explanation is really simplified

By Guru on   8/27/2010 10:16 AM

Re: Silverlight View Model Style: An (Overly) Simplified Explanation

@Dave Bartlett ... Although a great bonus of MVVM is the ability to hand the UI design to a "designer", in my experience the biggest case for MVVM is that it lends itself very well to testing!

By Ben on   8/29/2010 3:52 PM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use