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

Download this episode

Download Video

The Discussion

  • User profile image
    KGB9

    Great session John!!!

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

    Thanks!

  • User profile image
    jopapa

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

  • User profile image
    EzaBlade

    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

  • User profile image
    KGB9

    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.

  • User profile image
    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

  • User profile image
    ted

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

  • User profile image
    Surya

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

  • User profile image
    jopapa

    Thanks for the interest!

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

  • User profile image
    Paul

    Thanks John, your shows have been a great help

  • User profile image
    Anil

    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

  • User profile image
    jopapa

    @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.

  • User profile image
    Anil

    @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

  • User profile image
    jopapa

    @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.

  • User profile image
    Kyle

    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!
     
     
     

  • User profile image
    jopapa

    @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.

  • User profile image
    juanfranbla​nco

    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

  • User profile image
    emnu71

    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.

     

  • User profile image
    Jim

    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?

  • User profile image
    jopapa

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

  • User profile image
    jopapa

    @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).

  • User profile image
    Jim

    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.

  • User profile image
    EzaBlade

    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.

  • User profile image
    deepeshm

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

  • User profile image
    EzaBlade

    @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.

  • User profile image
    Jeff

    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

  • User profile image
    Jeff

    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 
     

  • User profile image
    Houman

    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

  • User profile image
    ColinBlair

    @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.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.