views:

1303

answers:

6

[Disclaimer: I'm ASP.NET MVC Developer]

Hi,

I'm looking for some best practices with implementing MVVM pattern with WCF RIA in Silverlight 4.

I'm not looking to use MEF of IoC for locating my ViewModels. What I would like to know is how to apply MVVM pattern with Silverlight 4 and WCF RIA.

I don't want to use other stuff like Prism or MVVM Light toolkit. I found many examples on Internet showing how it is wonderful to drag and drop a datasource on the view and the job is done (it reminds me about my first VB6 developments).

I tried to implement MVVM with WCF RIA and it's not strightforward at all. If I understand, the MVVM should contain all the logic in order to unit test it in isolation but when it comes to combine it with WCF RIA it's another story. I have the following questions.

  1. Can I use a generated metadata as model ? It would be easier to use it that if I write all from the scratch.

  2. As I saw the only way I could get data is through DomainContext or through direct binding in the view (local ressource). I don't want the direct binding in the view, not testable at all. On the other hand I can't use DomainContext, it doesn't expose any single entity !!! All I have is the EntitySet that I can bind to datagrid. How do I bind a single Entity to the DataForm from the ViewModel ?

  3. How do I udpate the model to the database ?

  4. How do I navigate from one Entity to a collection of it's items. For example if I have a Company Entity I would like to show a DataForm to update an entity informations and a datagrid to show companies adresses. When saving a form, I would like to save an information to Company and an information to adress about which adress was selected as active.

Please help me understand how to do it well. Or maybe I should drop the WCF RIA and to do it with WCF from scratch ?

What do you think ?

+3  A: 

You might be interested in this session. It explains how to use the MVVM pattern with RIA WCF Services.

Larry Grass
would definitely recommend looking at the sample 'BookClub' code from this article. learnt a lot from it
Simon_Weaver
+1  A: 

I found this post useful:

http://www.astaticstate.com/2010/04/silverlight-4-using-mvvm-patter-ria.html

Dan
+1  A: 

Some random answers...

I don't think that MEF is particuarly well suited for Silverlight. It's primarily for desktop apps, and could be adapted for other uses where the plug-ins are in the local file system relative to the app.

MVVM requires that you understand roles. The "view" is your XAML and code-behind. The code-behind should handle events from the user control, but very little more than that.

The ViewModel holds the data that the user control will bind to. Generally, the ViewModel is bound to the View as its DataContext, so that everything in the form can databind to properties in the ViewModel. The ViewModel must implement INotifyPropertyChanged, and raise property changed events for every property that the form databinds to.

You'll probably want to create an ObservableCollection, using an EntitySet as your source. This will handle INotifyCollectionChanged for databinding purposes. If the entities in the EntitySet also handle INotifyPropertyChanged, then you're in good shape on databinding for collections.

You can create a property for an individual entity, and databind to that, assuming that change notification is also implemented (both for entity members, and for the entity property).

RIA Services will regenerate the DomainContext on each build, which helps a little in keeping it in sync. It's intended to be a service layer above an ORM, though, so your ORM or other data mapping will still have to be maintained by other means.

I haven't looked at the final release of RIA Services, but I wasn't hugely impressed with the beta version. I'd rather have good entity classes defined on the server, and share them with the Silverlight project. It's not easy to set up, though, and requires some non-trivial WCF that doesn't rely on service referernces. (RIA Services final release may have cleaned some of this up, but the native WCF service reference in Silverlight is pretty much evil, mainly because it doesn't automatically recreate generated classes, and it hard-codes the URI for the server-side service.)

Metadata was another problem with RIA Services beta. It's easier to attach metadata attributes directly to your DataContract class and the individual DataMember properties, if you control the entity source. Again, that may mean not using RIA Services. Writing a separate metadata class, as was required for the RIA beta, wasn't a good solution.

I ended up not using RIA Services for Silverlight 3, and didn't regret it. Here's an excellent article on WCF and Silverlight. Although it says Silverlight 2, it's still on-target for any Silverlight release.

I do recommend MVVM Light. Source is available on Codeplex, if that's an issue. It provides messaging and commanding support, as well as a ViewModelLocator; while the latter takes a bit of work to understand, it's really a good extension to the basic MVVM model.

Hope this helps.....

Cylon Cat
There's also a nice running business apps sample using MVVM and WCF RIA. Check it out here: http://intersoftpt.wordpress.com/2010/06/29/clientui-part-5-the-supercharged-silverlight-navigation/
James
+2  A: 

Like many of the Silverlight developers around, I also found that the MVVM in Silverlight is quite tedious and difficult. Based on my experiences, I could tell that it's because Silverlight is simply not designed for MVVM yet. It doesn't have routed command, delegate command, or sufficient routed events that enables you to elegantly implement your apps in MVVM pattern.

The other frameworks such as MVVM Light, Prism, etc -- are simply too hard to use as you need to get familiar those several hundreds of interfaces, not to mention they only provide the architectural solution to the MVVM. This is certainly far less-than-ideal for business developers as they need to spend enormous effort and time on the architecture and plumbing stuff, while they should actually focus on what's matter most to their business and users. That said, the best MVVM solution would be the one that cover both architectural and visual (view) elements.

Since my company is strongly focusing on Silverlight UI, business lineups and MVVM; we've recently invented a new solution that significantly simplifies business apps dev using MVVM. We also cover many aspects of the navigation capabilities not available in Silverlight, such as multi-level navigation, page-level security and authentication, elegant busy-state management, and much more.

You can check out this post: http://intersoftpt.wordpress.com/2010/04/24/clientui-part-3-comprehensive-mvvm-framework-for-silverlight-development/

Hope this helps.

James
A: 

Just thought I would let you know about a project I am working on - just got our first release done. Provides a great simple way to approach MVVM for Silverlight + RIA Services specifically. Simplifies a lot of the MVVM stuff, and provides some more controller-like functions with the Notifications class. http://slmvvms.codeplex.com/

Ryan from Denver
A: 

I can only vote for Ryan's MVVM-S kit.

The MVVM/RIA piping is brilliant. As soon as you get the hang of the pipeline, the RIA bit becomes very easy, and efficient.

Just waiting for the release of the PagedEntitySetCollectionView class now :-)