Question

It's a simple question and I searched the Internet for hours without success...

I have a model and a view model with one property. To make this property viewable in the view, I use a view-model-object which should automatically be generated from the model-object and vice versa. Of course, the following code will throw an StackOverflowException, because the updating of the model-object in the model causes an update of the view-model-object in the view-model and this causes an update of the model-object in the model and so on...

class ModelObject
{
  ...
}

class ViewModelObject
{
  ...
}

class Model : INotifyPropertyChanged
{
  private ModelObject modelObject = new ModelObject();

  ...

  public ModelObject ModelObject
  {
    get
    {
      return this.modelObject;
    }
    set
    {
      this.modelObject = value;
      this.NotifyPropertyChanged("ModelObject");
    }
  }
}

class ViewModel : INotifyPropertyChanged
{
  private ViewModelObject viewModelObject = new ViewModelObject();
  private Model model = new Model();

  ...

  public ViewModel()
  {
    this.model.PropertyChanged += new PropertyChangedEventHandler(this.propertyChangedEvent);
  }

  public ViewModelObject ViewModelObject
  {
    get
    {
      return this.viewModelObject;
    }
    set
    {
      this.viewModelObject = value;
      this.model.ModelObject = new ModelObject(...);
      this.NotifyPropertyChanged("ViewModelObject");
    }
  }

  private void propertyChangedEvent(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName.Equals("ModelObject"))
    {
      this.ViewModelObject = new ViewModelObject(...);
    }
  }
}

What is the common way to synchronize these two objects?

Was it helpful?

Solution

There is no silver bullet. As model is a representation of the database and viewmodel is more closer to the interface, there is always some business logic needed to convert the model to view model and vice versa.

I usually have two methods in my view model class - SyncModel(ViewModel viewModel) and SyncViewModel(Model model)

One more suggestion -

Model should not implement INotifyPropertyChanged. The view model should implement this as its bound to the user interface. Why does the model ever need to change? It represents whats in the db. You can refresh it but why do you need change notifications for the model?

Edit: MVVM: Binding to Model while keeping Model in sync with a server version

Hard reference. Each class having a reference to another, listens to property change event and updates itself accordingly.

Observer Pattern - Have an observer class, each class register itself with an observer, observer listens for any changes and updates all its subscribers.

There's also an event aggregator which might be useful.

If you want a deferred update, an isDirty property would be required. You know your application better, google for more articles and choose wisely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top