views:

67

answers:

1

Hi there,

I've got a question about a Silverlight WCF Databinding pattern:

There are many examples about how to bind data using {Binding} expressions in XAML, how to make async calls to a WCF service, set the DataContext property of a element in the UI, how to use ObservableCollections and INotifyPropertyChanged, INotifyCollectionChanged and so on.

Background: I'm using the MVVM pattern, and have a Silverlight ItemsControl, whose ItemsSource is set to an ObservableCollection property on my ViewModel object. My view is of course the XAML which has the {Binding}. Say the model object is called 'Metric'. My ViewModel periodically makes calls to a WCF service that returns ObservableCollection. MetricInfo is the data transfer object (DTO).

My question is two-fold:

  1. Is there any way to avoid copying each property of MetricInfo to the model class - Metric?
  2. When the WCF calls completes, is there any way to make sure I sync the items which are in both my local ObservableCollection and the result of the WCF call - without having to first clear out all the items in the local collection and then add all the ones from the WCF call result?

thanks, Krishna

+1  A: 

1) I have done the mapping through a constructor like this:

public Metric(MetricInfo metricInfoDTO)

then map the properties from the DTO to the entity which of course is what you are trying to avoid. Yes, this is a bit of work but for me it has worked out very well. The alternative could be to use a object mapper like AutoMapper

2) I suppose you could have some kind of comparison logic to do updates and inserts into the collection. For me, I have done the clear and add which you describe in you question. It's simple, short and I haven't had any issues with it.

DaveB
Thanks Dave. I did go with some logic to do updates/inserts into the local collection. I was hoping to avoid that though. Looking at all the samples around the web - you'd think it is too simple - but in the end, for real world scenarios, we do end up doing a lot more of the plumbing in spite of a good data binding framework in Silverlight/WPF.
Krishna