Tag Archives: MVVM

Code Coverage for Silverlight and Windows Phone

As part of building my WP7 app, I spent a fair amount of time trying to get code coverage working for my unit tests. I tried a number of different approaches including using the Silverlight Unit Test Framework and NUnit + dotCover. I finally landed on an approach that worked for me using the unit testing capabilities in Visual Studio 2010. Since this is a much confused topic with very little info on the web on how to successfully get working for Silverlight/Windows Phone apps, I published a screencast on the subject.  I hope you find it useful.

Direct link:

https://channel9.msdn.com/posts/Code-Coverage-for-Silverlight-and-Windows-Phone-ViewModels

From http://en.wikipedia.org/wiki/Code_coverage:

"Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested."

In this screencast, you will learn how to get code coverage for your Silverlight and/or Windows Phone ViewModels through unit testing.  You can learn more about code coverage in Visual Studio 2010 from:

http://msdn.microsoft.com/en-us/library/dd537628.aspx

Get Microsoft Silverlight

WPF and DataAnnotations

The System.ComponentModel.DataAnnotations namespace was introduce in the .NET Framework 3.5 SP1 to support ASP.NET Dynamic DataSilverlight + WCF RIA Services now uses it as well.  I’ve been asked by a few people when WPF will use it.  Why wait?  Karl Shifflett has a sample of how to do it here:

https://www.vimeo.com/11585675

This video is part of a series of videos Karl has covering his Stuff sample application.  Stuff is a sample application Karl put together to demonstrate “WPF Line of Business using MVVM” .  It’s definitely worth checking out:

http://karlshifflett.wordpress.com/2010/05/09/stuff-wpf-line-of-business-using-mvvm-video-tutorial/

FOLLOW UP: DevDinner – Silverlight 4, WCF RIA Services, MVVM

Ok, this is a first!  I have always had good intentions of getting the follow up material for a developer dinner out before the dinner started.  However, my schedule inevitably has gotten in the way.  I can finally say MISSION ACCOMPLISHED.  Here are the decks and sample downloads:

Silverlight 4 Deck

WCF RIA Services Deck

SketchFlow & MVVM Deck

MVVM Sample App

WCF RIA Services + ViewModel Sample App

NOTE: The WCF RIA Services + ViewModel Sample App uses the MVVM approach from http://tinyurl.com/riasvcs-viewmodel.  Brad Abram’s code was written using an older version of WCF RIA Services.  There were a few breaking changes in the November release.  I went ahead and ported the MVVM and Unit Testing helpers to work with the November release of WCF RIA Services.  In the sample, ViewsHome.xaml does not use MVVM.  It uses DomainDataSource embedded in the XAML.  ViewsMvvmHome.xaml implements the same scenario, but using the MVVM approach Brad describes in the link above.

EventToCommand + DataStateBehavior eases MVVM pain

 

MVVM is a pattern.  There are all sorts of MVVM frameworks popping up that help make implementing the pattern easier.  If you select the MVVM tag off of www.codeplex.com, you will find a number of them:

http://www.codeplex.com/site/search?TagName=MVVM&ProjectSearchText=%22MVVM%22

One I have recently grown fond of is http://mvvmlight.codeplex.com/.  The reason I like this one is because they have built an Expression Blend Behavior called EventToCommand.  Details for the behavior:

http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

As the post says, ‘EventToCommand is used to bind an event to an ICommand directly in XAML.”

This is WAY COOL!!!  EventToCommand helps overcome commanding challenges in Silverlight.

I love Blend Behaviors.  If you haven’t looked into them, YOU MUST!  Every WPF and Silverlight developer should be using them (where applicable).  “A Behavior is in essence a reusable piece of interactivity that can be applied directly to user interface elements…”

Here are some resources:

Good Overviewhttp://tinyurl.com/BehaviorsOverview

More readinghttp://tinyurl.com/BehaviorsTriggersActions

Expression Blend 3 SDKhttp://tinyurl.com/blend3sdk 

Lots of folks creating and uploading them to Expression Galleryhttp://tinyurl.com/BehaviorsGallery

Behaviors from the Blend Teamhttp://expressionblend.codeplex.com/

Behaviors are part of the Blend SDK.  There is a lot of confusion about the Blend SDK because people think you have to buy Blend to use it.  You don’t!  You can download it from http://tinyurl.com/blend3sdk and use it straight from Visual Studio.  Blend definitely makes wiring up Behaviors easier so you should it for that if you are already using Blend!

The samples you can download from http://expressionblend.codeplex.com/ include DataStateBehavior and DataStateSwitchBehavior.  Peter Blois has a good post on these behaviors:

http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html

DataStateBehavior/DataStateSwitchBehavior provide a nice clean way to change values in your ViewModel that result in initiating animations in your View.

I’ve been wanting to whip up a simple sample that shows both EventToCommand and DataStateBehavior/DataStateSwitchBehavior.  Unfortunately, I don’t have the time right now to do so.  Instead of sitting on this, I decided to blog about it first, then share a sample/video when I get around to it.

Prism Visual Studio Templates (including MVVM)

Prism is:

“The Composite Client Application Guidance is designed to help you more easily build modular Windows Presentation Foundation (WPF) and Silverlight client applications.”

What most people find, once they take the time to dig in to Prism, is that it is MUCH more.  Prism has guidance on how to build an app that targets both Silverlight & WPF, has some plumbing that makes MVVM easier in Silverlight apps by providing a basic commanding implementation, etc.  However, the learning curve to get proficient with Prism can be a turnoff for some.  Even once you “get it,” you then realize that there is a fair amount of Visual Studio solution/project setup just to get to the point of being able to start building your parts of the application.  The good news is that David Hill has just released some templates for Visual Studio 2008 to make the basic setup for your Prism based apps much easier:

http://blogs.msdn.com/dphill/archive/2009/05/29/prism-quick-start-kit.aspx

http://blogs.msdn.com/dphill/archive/2009/06/15/prism-quick-start-kit-update.aspx

I just went through the walkthrough in the first link.  Here is what you get after the “Prism Shell” and “Prism Module” templates:

image

You get the skeleton of a basic composite application using MVVM.  I think this is a big time saver.  I will definitely be keeping an eye out as David evolves the templates / quick start kit.

UPDATE: I was so excited to share my discovery of David’s work that I forgot to add a few more helpful resources I have found:

Code Snippet for Prism DelegateCommand

A code snippet to quickly write Prism commands