Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Silverlight Firestarter 2010 Session 4 - MVVM: Why and How? Tips and Patterns using MVVM and Service Patterns with Silverlight and WP7

Download

Right click “Save as…”

Silverlight Firestarter 2010 Session 4 - MVVM: Why and How? Tips and Patterns using MVVM and Service Patterns with Silverlight and WP7 by John Papa

Download the slides and code here.

Recorded live as part of Silverlight Firestarter 2010 in Redmond, WA on Dec 2nd 2010

Silverlight Firestarter Agenda
- Silverlight Firestarter 2010 Keynote with Scott Guthrie
- Session 1 - Data Binding Strategies with Silverlight and WP7 by Jesse Liberty
- Session 2 - Building Compelling Apps with WCF using REST and LINQ by Yavor Georgiev
- Session 3 - Building Feature Rich Business Apps Today with RIA Services by Dan Wahlin
- Session 4 - MVVM: Why and How? Tips and Patterns using MVVM and Service Patterns with Silverlight and WP7 by John Papa
- Session 5 - Tips and Tricks for a Great Installation Experience by Tim Heuer
- Session 6 - Tune Your Application: Profiling and Performance Tips by Mike Cook & Jossef Goldberg
- Session 7 - Performance Tips for Silverlight Windows Phone 7 by Jaime Rodriguez
- Session 8 - The Roadmap for Media with Silverlight

Tags:

Follow the Discussion

  • Great session John!!!

    Any chance of getting code samples for the reusable services classes?

    Thanks!

  • John Papajopapa Evangelist on the Loose

    @KGB9:Thanks for the feedback! We're going to post the slides and code samples this week (my target). I'm gathering them now.

  • I keep starting MVVM projects but cannot seem to keep them that way!! I am desperate to get my head around it so I am keen to see the code if possible.

    Thanks

  • Thanks John!

    This was one of the most important sessions to a lot of us trying to use the MVVM pattern and the right way of doing it. I wish you would have had this topic when you were in Tampa.

    Thank you again for all you do in teaching us Silverlight.

  • pardeep pardeep

    Hi john
    I read you many books like Data driven services with silverlight2.0 etc.But from SL2.0 to SL 4.0 there is lot of change, hope we will see a new book from your side soon.
     Now we are implemanting MVVM adn this webcast is very intersting ,  please share code of this demo ASAP so that we can have a base idea of MVVM+ services sepration 
     
    thanks
    pardeep kaushik

  • tedted

    Hi, John,
    I want to download your sample and slide, could you provide it?
    Thanks!
    Ted

  • SuryaSurya

    Thanks John :)
    It is really great help session to us. With this session I came to know lot of technics in MVVM.

  • John Papajopapa Evangelist on the Loose

    Thanks for the interest!

    I updated the post to include a link at the top to all slides and demos from the sessions. Enjoy!

  • PaulPaul

    Thanks John, your shows have been a great help

  • AnilAnil

    Hello John
    Great video....i have a question about MVVM....is it ok to have more than 100 lines of code in the view model or Is it recommended to break a view model when its code is growing?
    Thanks

  • John Papajopapa Evangelist on the Loose

    @Anil: Thanks. Short answer is "yes". My suggestion is to examie the 100 lines of code and fsee if any of those can be rduced. Look for repetitive tasks that can be refactored within the class. THen look for tasks that may be desired outside of your view model and used by other VM's too. If there are any, then these are great candidates for a "service" that the VMs can use. Examples are interaction with Isolated Storage, Navigation, serialization, communicating with WCF, etc.

    I would be less worried with lines of code than if your code is reusable and maintainable as it can be.

  • AnilAnil

    @jopapa:Got it...Thanks for the reply John...One more question..you mentioned to use behaviors for listbox selection changed events...but that behavior cannot be most likley reused anywhere else which does not satisfy the purpose of a behavior...
    Instead...i think its easy to invoke the command in the selection changed event in the code behind as opposed to writing 5 lines of XAML for behavior...I know its slightly against MVVM...
    An thoughts on this?
    Thanks

  • John Papajopapa Evangelist on the Loose

    @Anil: A behavior itself is reusable and allows you to bind the event to the method in the viewmodel that you want to use. You can use the InvokeCommadnAction behevior, for example.

    In SL 5 I would use a markup extension.

    I would not use a event handler in the code behind over a behavior.

  • KyleKyle

    Hi John,
    So what exactly would you put in the code behind (if anything)?  Coming from the ASPX world I'm still wrestling with seperating out the view model logic from the code behind.  Thanks very much for the demos and this firestarter has been great for getting a better understanding at the intent behind a lot of the controls.  I'll be watching silverlight TV regularly!
     
     
     

  • John Papajopapa Evangelist on the Loose

    @Kyle: Let me first start by making it clear that having code in the codebehind is not a bad thing. However, when I start coding I find most of my logic (if/then, commands, actions, etc) all happen in the viewmodel. These are all logic switches, conditionals, and some sort of "smarts". Sometimes we have code that is very specific to the presentation and has to reference a control or do something in the UI. An example of that is running an animation or a specific visual state. When I write my code I find that rarely do I have code in the code behind.

    Thin kof ASPX codebehinds with WebForms and how it is often better to separate logic from there into a controller of sorts. Similar analogy.

  • Hi John,

    Great session, very clear and useful. I will use it from now on as a point of reference when evangalising MVVM. Nice to see areas like Services,  Design etc and even dependency injection implemented in a lightweight format.

    Juan

  • Hi John,

    Nice work ! Very clean implementation of MVVM, and brought in a very bright and understandable way, very nice to follow your session here !

    I've downloaded the code, but i've got some minor reference problem, seems like missing next DLL's :

     

    -Microsoft.Expression.Effects

    -Microsoft.Expression.Interaction

    - System.Windows.Interactivity

     

    These give next errors :

     

    Error 55 The property 'Triggers' does not exist on the type 'StackPanel' in the XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 21 30 BookShelf
    Error 56 The tag 'EventTrigger' does not exist in XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 22 34 BookShelf
    Error 57 The tag 'InvokeCommandAction' does not exist in XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 23 38 BookShelf
    Error 58 The property 'Triggers' does not exist on the type 'StackPanel' in the XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 31 30 BookShelf
    Error 59 The tag 'EventTrigger' does not exist in XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 32 34 BookShelf
    Error 60 The tag 'InvokeCommandAction' does not exist in XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\AdminView.xaml 33 38 BookShelf
    Error 61 The property 'Triggers' does not exist on the type 'ListBox' in the XML namespace 'http://schemas.microsoft.com/expression/2010/interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml 140 27 BookShelf
    Error 62 The tag 'EventTrigger' does not exist in XML namespace 'http://schemas.microsoft.com/expression/2010/interactivity'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml 141 28 BookShelf
    Error 63 The tag 'EventToCommand' does not exist in XML namespace 'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4'. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml 142 29 BookShelf
    Error 64 Could not load file or assembly 'System.Windows.Interactivity, Version=4.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Het systeem kan het opgegeven bestand niet vinden. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml 1 1 BookShelf
    Error 65 System.IO.FileNotFoundException was thrown on "H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml": Could not load file or assembly 'System.Windows.Interactivity, Version=4.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Het systeem kan het opgegeven bestand niet vinden. H:\Dev\SilverLight4\SilverlightFirestarter\Src\All SLFS10 Session\SLFS10 Session 3 (WCF RIA Services) - Code\WCFRIAServices_Firestarter\RIAServicesWithRepositoryDemo\BookShelf\Views\BookView.xaml 1 1 BookShelf

    Should i install Expression Blend or something like that, or where can i find the DLL's seperatly, as it seems they are not included in the zipped package.

    Thx for any response,

     

    Emmanuel Nuyttens

    .NET Architect from Belgium.

     

  • JimJim

    Hi John, a quick question,, let's say I have a few VM and each of them may be shared by more than one view.  So do I create a few viewmodellocator or there's other alternative?

  • John Papajopapa Evangelist on the Loose

    @emnu71:You can get those DLL's from the trial version of Blend. Easiest way, probably.

  • John Papajopapa Evangelist on the Loose

    @Jim: Just 1 VM Locator is needed. You could change the code in your VM Locator to create a new instance of the VM every time, if that  is wha tyou want for your Views. If you want to share the VM, just have you cod ein the VM Locator create one instance of the VM and then the getter always returns that. It's up to you in how you want to share them (or not).

  • JimJim

    Hello John,
    I downloaded and ran you the demo app.  Can you teach me how I can prevent user from triggering save operation when there's error on the edit child window.  Currently it allows user to click submit and returns "unsucessful" message but then it crashes after.
    One more question, I followed the same aprroach to put jpg in images folder but somehow I can't display the image.  Any clue what might have gone wrong?
    Thanks for your prompt reply.

  • I want to use Windows Authentication and so I changed the code in the BookClubService:-

        [RequiresAuthentication]
        [EnableClientAccess()]
        public class BookClubService : LinqToEntitiesDomainService<BookClubEntities>
        {

    and used this in the App.xaml.cs file:-

    webContext.Authentication = new WindowsAuthentication(); 
    

    Now the service will not work because I am unauthorized. Why does this not work? At the moment I am experimenting on my home PC.

  • @EzaBlade - have you made the change in the Web.Config?

  • EzaBladeEzaBlade

    @deepeshm:
    No I had not done that - thanks. Now it will display books without error but the Edt button is disabled even though it's Enabled property is bound to the User.IsAuthenticated property in the xaml.

  • JeffJeff

    I am still confused about the relationship between WCF and RIA.  
    My application needs to support a silverlight client as well as non-silverlight clients such as android.  I'd like to implement the biz logic in one place, presumably running server side and accessable by WCF.  Does it make sense for the silverlight app to connect to those WCF services via RIA while the andriod client consumes those same WCF services by the traditional wsdl contract?
    Your suggestions or pointers to material on this subject are greatly appreciated.
    Cheers,Jeff

  • JeffJeff

    re: still confused above:  
    Someone pointed me to the following Silverlight TV Video where John & friends answer my question perfectly:
    http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-26-Exposing-SOAP-OData-and-JSON-Endpoints-for-RIA-Services/ 
    see also: http://forums.silverlight.net/forums/p/218286/520249.aspx#520249
    Basically the answer is:  Yes I can develop my silverlight app using RIA and also provide wcf endpoints for OData, JSON, SOAP, etc.
    Cool!!Jeff 
     

  • Hello John,

     

    This was a fantastic presentation. Finally I have seen a way to encapsulate the DomainContext properly within a Service and expose only the CRUD operations to the client rather than the whole context.

    So far so good, however I think you made it a bit too convienent leaving out the actual Delete, Add and Update the entities. Devil

    I have downloaded your code, but if I added a book to the Observablecollection within the ViewModel, that book is not added automatically to the Context.Books, same goes with Delete or Update, hence the whole Service concept breaks the state change trackability of RIA objects.

    But in this example it seems the way the ViewModel's observable collection is populated, it has no connection to the context on the Service. Which makes sense in a way, since Services should be stateless.  By having this in mind, perhaps it is not a good idea to encapsulate the DomainContext within a Service after all.

    What is your opinion on this, how would you extend your example to support Adding, updating and deleting books through the Service?

     

    Many Thanks,
    Houman

  • @Houman

     

    If you dig deeper into John's code you will find that the ObservableCollections returned by his service are really WCF RIA Services SP1 Beta EntityList objects. Unlike a plain ObservableCollection, an EntityList is linked to the DomainContext's EntitySet. That means anything you add or remove from the EntityList is also being added and removed from the EntitySet. It also means that if an entity is deleted outside of your EntityList, from a second EntityList or from the DomainContext itself for example, it will be removed from your EntityList as well preventing zombie entities from eating your application.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.