3

the problem is quite simple : Basically my View should display data coming from a WCF service. The data is updated asynchronously with an high frequency, so the GUI should be updated accordingly when the data on server side changed.

The View is showing a lot of data, so basically it's binding 50/60 fields exposed in the ViewModel.

The Model part is basically a POCO object which contains the 50/60 fields displayed in the GUI. Yes, the ViewModel is exposing the Model object to the View. (Note that the Model object implements INotifyPropertyChanged, in order to notify the View when a property value changed.)

Now, I wrote a "DataService" layer which interacts with the WCF service, and it's responsible of updating the Model according to the data returned by the WCF service. When the data is updated on server side, the WCF service, for performance reasons return to the client only the set of fields that are changed.

So, in the DataService there is an event handler which manages the wcf service updates like the following :

void OnServiceUpdated(UpdateArgs args)
{
  foreach(Field field in args.ChangedFields)
  {
      if(field.Key == "BetName") _modelBet.BetName = field.Value;
      else if(field.Key == "BetUser") _modelBet.BetUser = field.Value;
      [...]
      //the same for 50 fields...
  }
}

Now this horrible code which update the model, is needed because we want to update only the fields of the model which are not changed on server side. (Note that the wcf service API cannot be changed).

My question is : What do you suggest in order to improve the performance of the "OnServiceUpdated" handler?

Thanks in advance, Jhon

1 Answers1

2

You say "The data is updated asynchronously with an high frequency"

As I understand you don't want frequent effect on UI. So you may use dispatcher timer for late notification. It works on UI thread ,different then other timers.

http://msdn.microsoft.com/tr-tr/library/system.windows.threading.dispatchertimer.aspx Here is an answer of SO user https://stackoverflow.com/a/15044392/413032 about it.

If you use MVVM so if I were you I made args.ChangedFields names and my ViewModel property names equal. So by reflection or using an auto-mapper I can transfer property values to viewModel properties.

You can also after all-properties set up rise an event and update UI.

For providing this ;

As I understand you use binding so binding has modes and updatesourcetrigger option so you can updatesouce explicitly .

http://msdn.microsoft.com/en-us/library/cc278072(v=vs.95).aspx#updating_the_data_source http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger.aspx

Hope show you a way.

Community
  • 1
  • 1
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • Hi Davut,many thanks for your answer. Ok, thanks for suggesting the Dispatcher TImer, but the frequent updates on the UI are not an issue in this case. – user1070316 Mar 05 '13 at 21:23
  • Hi Davut,many thanks for your answer. Ok, thanks for suggesting the Dispatcher TImer, but the frequent updates on the UI are not an issue in this case. Btw, I will treat this problem separately. Yes, I already thinked at the solution you proposed, so by using the reflection in order to transer property values to viewModel properties, but for perfomance reason I'd like to avoid to use reflection. It's very interesting your suggestion about the Auto-Mapper. I'll try to investigate on that direction. Thanks, John – user1070316 Mar 05 '13 at 21:31
  • Some people say this http://stackoverflow.com/a/8111631/413032 method works faster than reflection. Beside I do not think reflection cause problems. Binding uses reflection and 1000 Textblock object in a control bound under 1 second http://msdn.microsoft.com/en-us/library/bb613546.aspx (render time 1.2sn). Sure you are free of using auto-mappers http://www.codeproject.com/Articles/61629/AutoMapper. It really depends on your problem. – Davut Gürbüz Mar 06 '13 at 07:14