开发者

What is a good pattern for binding a collection of objects coming from WCF, in Silverlight?

开发者 https://www.devze.com 2022-12-28 10:35 出处:网络
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 s

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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消