8

When using .NET RIA Services and MVVM in Silverlight 3.0 is there a difference between the Metadata type from RIA Services and the ViewModel from the MVVM pattern? Are these the same thing or should they be keep separate?

The metadata type is a sealed internal class to the partial Entity class. There doesn't seem to be a proper separation there but the metadata type can also be decorated with attributes for Validation which makes it look like a ViewModel.

I've searched around but I didn't see anything that talks about this in any detail.

Brad Tutterow
  • 7,487
  • 3
  • 33
  • 33
Robert Kozak
  • 2,043
  • 17
  • 33

2 Answers2

11

Agree with ChuckJ - generally the DomainContext forms part of a view model. For example, say I had a search page that allowed searching against a product catalog. Here is how I'd structure things:

On the server:

class Catalog : DomainService {
    IQueryable<Product> GetProducts(string keyword) { ... }
}

The generated DomainContext:

class Catalog : DomainContext {
    EntityList<Product> Products { get; }
    void LoadProducts(string keyword);
}

The view model I would write:

class SearchViewModel {
    Catalog _catalog = new Catalog();

    public IEnumerable<Product> Results {
        get { return _catalog.Products; }
    }

    public void Search(string keyword) {
        _catalog.Products.Clear();
        _catalog.LoadProducts(keyword);
    }
}

And then finally in my xaml, I'd set my UserControl's DataContext to be an instance of SearchViewModel, and bind an ItemsControl to the Results property. I'd use the ViewModel pattern of your choice to bind a button click to Search (which is effectively a command that SearchViewModel exposes). I personally like something that I have working with Silverlight.FX as in:

<Button Content="Search"
  fxui:Interaction.ClickAction="$model.Search(keywordTextBox.Text)" />

and as initially shown here.

As Chuck mentions I might indeed have other state in my view model, for example, the SelectedProduct that might be two-way bound to the SelectedItem of a ListBox in my xaml, and then bind the same SelectedProduct as the DataContext of a DataForm to show details of a selected product.

Hope that helps! I'll be blogging about this some more on my blog soon.

Nikhil Kothari
  • 5,215
  • 2
  • 22
  • 28
  • After some thought over the past week I think this is what I was thinking. The DomianContext is not the complete ViewModel but a part of it. Will the attributes defined in the MetadataType of the DomainService (like [Required]) have to redeclared in the ViewModel or the fact that the DomainContext is part of the ViewModel enough to be pickup by the validation engine? – Robert Kozak Apr 20 '09 at 16:03
  • Link to Nikhil's blog post of the subject : http://www.nikhilk.net/Entry.aspx?id=229 – Robert Kozak Apr 20 '09 at 16:20
  • The [Required] etc. metadata attributes are on the entity types - not on the view model itself. They will be picked up if you expose an instance of the entity from the view model (eg. Products in the view model sample above). – Nikhil Kothari Apr 24 '09 at 01:26
  • is it clever enought to access the `DomainContext` right from the VM? or else I should wrap it up in some kind of repository, or do it with a UoW? Can you refer me to a good article? Unless you think querying and manipulating data from the VM right to the DomainContext is fine. – Shimmy Weitzhandler May 12 '11 at 23:33
  • Actually I just found [the exact answer](http://stackoverflow.com/questions/665204/how-to-combine-net-ria-services-with-mvvm-in-silverlight-3/666532#666532) to my scenario. – Shimmy Weitzhandler May 12 '11 at 23:40
1

The RIA services data context was designed to play the role of a ViewModel in the MVVM pattern since they natively support data binding, but they don't use that term in their documentation. However, it really depends. You will probably need state in your view model than the RIA data context provides such as commands and other view related state. I think what you want to do is use the data contexts from the RIA services as a part of the view model.

chuckj
  • 27,773
  • 7
  • 53
  • 49
  • The more I look into it I think I want to hide the EntityObject and its MetaDataType as much as possible. I think you are right that I probably want the DataContext in the ViewModel but also I want to remove the MetaData internal class from the EntityObject and move it to the ViewModel. Correct? – Robert Kozak Apr 15 '09 at 19:46
  • Wait. That doesn't sound right either. I think the MetaData class needs to be on the server side and the ViewModel on the Client. Ugh. Maybe the ViewModel belongs as Shared code? – Robert Kozak Apr 15 '09 at 20:28
  • After reviews answers this morning I finally realized you answered this question. Hi Chuck, long time no see. Hope life is treating you well. – Robert Kozak Apr 20 '09 at 15:58