Kung Fu Silverlight at PDC 2010: Patterns and Practices with MVVM and RIA Services | John Papa

John Papa

Evangelist on the loose

Kung Fu Silverlight at PDC 2010: Patterns and Practices with MVVM and RIA Services


Today you can watch my PDC 2010 session titled “Kung Fu Silverlight: Tips and Architectural Patterns and Practices”. Here is the summary of the session:

Learn about the rewards of using RIA Services together with development patterns, such as the Service Providers, Single Responsibility pattern, Commanding, user Interactions, Messaging, ChildWindows, Design Time Data, Testing, and developing using the Model-View-ViewModel (MVVM) pattern, to build Silverlight and Windows Phone applications. Hear the top tips you need to know for building data driven Silverlight applications that solve real world problems.

The session covers quite a bit, and I plan on expanding on these topics in the near future on Silverlight TV and at the Silverlight Firestarter on Dec 2, 2010. The point I try to get across in this session is that you can build a Silverlight application using consistent patterns without having to be a rocket scientist. I demonstrate many of the common patterns used with Silverlight applications and show both how to use them and also why they are helpful/important.

For example, I show how I use a ViewModelLocator and abstracted services to make it easy to see design time data … but more importantly, I talk about WHY I do this. I am not a designer, but there are key elements to design time data that help me test my code at design time.

Spoiler alert!: I also intentionally do not show RIA Services until the very end. You will notice that I have the full power of RIA Services in this application without compromising any of my architecture. One of the keys here is using Separation of Concerns (SoC) by abstracting my services from the ViewModel. Please watch to find out more Smile

I’ll go deeper into each of these topic in a series of posts and Silverlight TV episodes in the near future. But for now check out this talk and if you take anything away from it please grasp that

  1. Separation of concerns is good
  2. You can do MVVM and get all the goodness that comes with it
  3. Design time data is immensely helpful
  4. RIA Services fits into MVVM
  5. John will follow up with deeper dives

The full deck is available online at the PDC link, or you can grab the deck by clicking the image below …



… and you can grab the full source for the demo app I showed by clicking this image  …



Note: I want to thank the WCF RIA Services team for working with me and accepting my feedback on how RIA Services integrates with MVVM and other patterns. They have been very supportive of integrating their platform with patterns and continue to look for ways to make it more powerful.

tags: Silverlight
  • Anonymous

    The Kung Fu graphic link near the bottom of the page seems to be broken

  • Anonymous
  • Anonymous

    John, where one can get such styles as you are using in your book app?

  • Anonymous

    Will the video be available for download?

  • http://brandonzeider.me Anonymous

    I like the way you abstracted RIA services, but I still feel dirty having data access logic (queries) in my client layer. Do I just need to get over it? :)

  • Anonymous

    how can I download full video?

  • http://johnpapa.net JohnPapa

    Mark & Anon – I fixed the link. Sorry about that. For some reason it did not work when I pointed it to a pptx file, so I zipped it.
    Alex – The style is a variant of the Cosmopolitan style in the new Silverlight theme packs.
    Frans & Mojam – I assume the video will be available for download after they have time to process them all.

  • http://johnpapa.net JohnPapa

    Brandon – It depends wha tyou are calling "Data access logic". The LINQ queries are not actual queries against a DB, as you I am sure you are aware. They are instructions on how the actual data access layer on the server should process the request. Instead of a web service method called ‘GetMyStuff()’ you have a query that allows you to get your stuff, but make little adjustments. These adjustments are really important. For example, if you needed to get your "stuff" but filter it a specific way and your web service method has no parameter for it, you have to change your interface for the web service. But with RIA Services you can just change the LINQ statement w/o any changes on the server.
    But either way, it’s not something I think is bad. I agree that data access layers should stay server side. I dont consider this data access. I consider it a client side data service, also known as "service agent" and "gateway" amongst other names.
    Thanks for the feedback.

  • Anonymous

    Hi John,
    Your presentaion was really great. Thanks for that.
    I cannot wait for you to explain all the concepts you introduced with more details on the Silverlight TV site.
    I had a lot of problems watching the streaming video from England. We cannot see the code clear. It is all blurred. It would be great if we could download it.
    Please let us know when you record the videos on Silverlight TV.

  • Anonymous

    Hi John,
    Great presentation, The part on abstracting RIA is really nice and we’ll be refactoring our code shortly. Another interesting part was your approach to dialogs. At the moment we have implemented a service which is responsible for showing dialogs but I really liked your messaging/eventing approach. So I have a question about sharing a data context between the two VMS.
    If I understand your presentation correctly you broadcast a message which has the “Selected” Book as the payload (which could be attached to the calling/broadcasting ViewModel’s datacontext) so the dialog is actually editing the entity in the BookShelfVM data context.
    Now, assume the dialog has a dropdown list bound to Categories (probably need to load Category list in child VM’s datacontext). So now we have set the Category property to a value which was not loaded as part of your initial query (i.e. Category XYZ was not returned in your original GetBooks().Include(..Categories) object graph).
    So we could just set the CategoryId of the object then the calling VM would need to load the Category (name, etc) once the use has closed the dialog.
    We achieve this now by (“sharing”)/passing in the datacontext to the child VM and via a callback from the service but I was wondering how we would achieve this using the messaging/eventing approach? I’ve never been 100% happy with our implementation of this and I’m looking for a better way..

  • Anonymous

    Very clear talk!
    Any changes to share the link for the code?
    John Kattestaart

  • http://devgroup.com.pl Anonymous

    Hi John,
    Your presentation gave me an oportunity to resolve my problems with abstraction layer which covers data application layer.
    I used ideas from your project and I am on the way to build my own prototype for Silverlight application with MVVM design patern.
    I think, one of the most interesting part is Repository Pattern on Entity Framework
    in asynchronous environment. It can save much time because I needn’t build
    another webmethods for every entity in your domain service class.
    I would be very grateful if someone expresses their opinion on this type of solutions you.
    Project is on codeplex: devprototype.codeplex.com

  • Anonymous

    Hi John,
    Thanks a lot for this great presentation!
    I’ve two remarks about the part on abstracting RIA.
    First of all, I think your ViewModel code does have a dependency on RIA
    services. It’s in the callback of the Save method. The
    IBookDataService.Save method has an Action<SubmitOperation> as argument.
    The type SubmitOperation is RIA specific. So the make it really abstract
    from RIA one has to create and use an interface here as well, right?
    Second, the ViewModel has, among others, the property Books. It is
    filled in the callback method GetBooksCallback. This works very nice
    since your application doesn’t support adding new books. Adding a new
    book would require adding a book to the ViewModel’s Books property and
    to the BookDataService (IBookdataService does not support this at the
    moment). Another approach would be to expose the book collection of the
    Model via the ViewModel to the View. What is the motivation for making
    the collection of books part of the ViewModel and not of the Model? How
    would you recommend to implement adding books in a MVVM-friendly way?
    Thanks for any feedback,

  • http://realworldsa.dotnetdevelopersjournal.com Anonymous

    What is going on with…
    Microsoft’s "strategy with Silverlight has shifted"

  • Anonymous

    When will the videos be available for download?

  • http://blogs.techrepublic.com.com/programming-and-development/?p=3311 Anonymous

    Pingback from Programming news: IronRuby 1.1.1, BlackBerry App World, Silverlight strategy | Programming and Development | TechRepublic.com

  • Anonymous

    Very good session!
    But, I didn’t find any session on SL Deployment!

  • http://johnpapa.net JohnPapa

    The videos are now available for download on the PDC site.

  • http://peternewhook.com Anonymous

    I get prompted for a TFS account uname and password when I open the sample.

  • http://peternewhook.com Anonymous

    Now I’m getting the following error because of the database (the copyable bit)
    at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
    at System.Data.EntityClient.EntityConnection.Open()
    at System.Data.Objects.ObjectContext.EnsureConnection()
    at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
    at System.Data.Objects.ObjectQuery`1.GetEnumeratorInternal()
    at System.Data.Objects.ObjectQuery.System.Collections.IEnumerable.GetEnumerator()
    at System.ServiceModel.DomainServices.Server.DomainService.Enumerate[T](IEnumerable enumerable, Int32 estimatedResultCount)
    at System.ServiceModel.DomainServices.Server.DomainService.Query(QueryDescription queryDescription, IEnumerable`1& validationErrors, Int32& totalCount)
    at System.ServiceModel.DomainServices.Hosting.QueryProcessor.Process[TEntity](DomainService domainService, DomainOperationEntry queryOperation, Object[] parameters, ServiceQuery serviceQuery, IEnumerable`1& validationErrors, Int32& totalCount)
    at System.ServiceModel.DomainServices.Hosting.QueryOperationBehavior`1.QueryOperationInvoker.InvokeCore(Object instance, Object[] inputs, Object[]& outputs)
    essentially "Cannot be opened because it is version 655…"
    I assume you created this on a computer with MS SQL studio or server installed? SQL express gets mad when that happens

  • Anonymous

    I liked your talk, but your demo app seems to have a major perf issue (I’ve seen similar in SL before).
    Once I log in, even though nothing is happening on the screen, it is sucking 60% of 4 of the 8 cores on my i7 laptop under Win7-32! CPU-sucking is not good for battery life.
    Any chance this can be fixed? I’ve seen some 100%-cpu bugs on the SL forums that are still unanswered by MS.
    Vic Klien

  • Anonymous

    I created a post about the excessive-CPU-use issue I’m seeing with this sample on the forums here: forums.silverlight.net/…/208709.aspx

  • http://www.xneterp.com Anonymous

    Hello John,
    Thank you for the first ever 300 Level training video, and for your implementation of the Service Agent…
    I have a small medical (but powerful now thanks to you) SL4 application at hat i would like to share with you and you can share with the people here witting on this post to discuss it and improve on it.. I use the service agent and Lazy loading to create CRUD… if people like my application, all thanks go to John Papa and his amazing set of videos, on Ria, EF4, XAML, SL4 , Lazy Loading, and MVVM
    my email is
    my first name @ yahoo dot com
    note: that i will only share my SL4 app if I you have seen this amazing video from John which will make you able to add value

  • Anonymous

    I second the question posted by
    Merijn de Jonge
    John , who would you add new books in a MVVM way in amazing tutorial application .. is the best Bizz training I’ve seen

  • dbl

    I really liked this presentation and architecture for the project. One thing I’m wondering about is how you would handle “pre-prompting” by this I mean if you had a Delete button how would you prompt the user to confirm they wanted to do the delete. Is there a way to “cancel” the binding similar to the way you would do it in javascript?

  • john

    dbl – Thanks. You could use the codebehind for a simple yes/no prompt. The MVVM police wont arrest you for it :-)
    But another way is to have the bound command fire a custom message off that the View intercepts, then presents the message, accepts the feedback, and returns the feedback via a message callback. MVVM Light supports this with its message callbacks.
    The former is easier and I can’t tell you its bad, cuz I dont think it is. The latter is more along the pattern. Your call of course.

  • aaron

    john what about just using a singleton shared servicecontainer that allows for messageboxes to be presented. Using an interface for the servicecontainer also allows for testability. And presents VERY little additional code. ????

  • john

    aaron – for my dialogs, I prefer using an IDialogService, very much like how Laurent Bugnion uses it in his MVVM Deep Dive video from MIX11. More code, but much more separatation and provides more flexibility on how to do it.

  • Prakash

    Hi John,
    Closest sample I seen to a real business app, Time for an update on the demo vid.
    New features request on same demo:
    1) IAnimationService for invocations of page transitions from viewmodel using mvvmlight messenger.
    2) IDialogService
    3) Dynamic Menu’s, something that can reflect the xaps you have on the server, but not download to build the menu in the main page.
    4) Dynamic downloading of xaps for menu option selected.
    If you got time… and please continue to maintain the simplicity in your design.
    Prakash, your humble student

  • http://medlink.codeplex.com/ Oscar Agreda

    Hello John
    I created a full Application using 90% of your MvvM Implementation
    Thank you for hiring a set of experts and putting together this awesome MvvM.

  • http://www.LinkedTraining.com.au Paul Linton

    I have significantly simplified my SL app by using this DataServiceAgent concept, thank you. I did find a problem with the sample, however. I think there is an issue if multiple requests are issued which use the same LoadOperation. I have modifed my equivalent of RunBooksQuery to be
    var bookLoadOp = Context.Load(query);
    bookLoadOp.Completed += (s,e) => getBooksCallback(bookLoadOp.Entities.Cast
    This creates a new LoadOperation for each data service call.

  • hossein moradi

    great presentation.
    but how can i implement delete method in this sample?

%d bloggers like this: