I've got a question about a Silverlight WCF D开发者_高级运维atabinding 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:
- Is there any way to avoid copying each property of MetricInfo to the model class - Metric?
- 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) 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.
精彩评论