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

In the past I ‘ve written an application that supports “live” editing of data objects from multiple locations: many instances of the app can edit the same object at the same time, and when someone pushes changes to the server everyone else gets notified and (in the simplest scenario) sees those changes immediately. Here’s a summary of how it was designed.

Setup

  1. Views always bind to ViewModels. I know it’s a lot of boilerplate, but binding directly to Models is not acceptable in any but the simplest scenarios; it’s also not in the spirit of MVVM.

  2. ViewModels have sole responsibility for pushing changes. This obviously includes pushing changes to the server, but it could also include pushing changes to other components of the application.

    To do this, ViewModels might want to clone the Models they wrap so that they can provide transaction semantics to the rest of the app as they provide to the server (i.e. you can choose when to push changes to the rest of the app, which you cannot do if everyone directly binds to the same Model instance). Isolating changes like this requires still more work, but it also opens up powerful possibilities (e.g. undoing changes is trivial: just don’t push them).

  3. ViewModels have a dependency on some kind of Data Service. The Data Service is an application component that sits between the data store and the consumers and handles all communication between them. Whenever a ViewModel clones its Model it also subscribes to appropriate “data store changed” events that the Data Service exposes.

    This allows ViewModels to be notified of changes to “their” model that other ViewModels have pushed to the data store and react appropriately. With proper abstraction, the data store can also be anything at all (e.g. a WCF service in that specific application).

Workflow

  1. A ViewModel is created and assigned ownership of a Model. It immediately clones the Model and exposes this clone to the View. Having a dependency on the Data Service, it tells the DS that it wants to subscribe to notifications for updates this specific Model. The ViewModel does not know what it is that identifies its Model (the “primary key”), but it doesn’t need to because that’s a responsibility of the DS.

  2. When the user finishes editing they interact with the View which invokes a Command on the VM. The VM then calls into the DS, pushing the changes made to its cloned Model.

  3. The DS persists the changes and additionally raises an event that notifies all other interested VMs that changes to Model X have been made; the new version of the Model is supplied as part of the event arguments.

  4. Other VMs that have been assigned ownership of the same Model now know that external changes have arrived. They can now decide how to update the View having all pieces of the puzzle at hand (the “previous” version of the Model, which was cloned; the “dirty” version, which is the clone; and the “current” version, which was pushed as part of the event arguments).

Notes

  • The Model’s INotifyPropertyChanged is used only by the View; if the ViewModel wants to know whether the Model is “dirty”, it can always compare the clone to the original version (if it has been kept around, which I recommend if possible).
  • The ViewModel pushes changes to the Server atomically, which is good because it ensures that the data store is always in a consistent state. This is a design choice, and if you want to do things differently another design would be more appropriate.
  • The Server can opt to not raise the “Model changed” event for the ViewModel that was responsible for this change if the ViewModel passes this as a parameter to the “push changes” call. Even if it does not, the ViewModel can choose to do nothing if it sees that the “current” version of the Model is identical to its own clone.
  • With enough abstraction, changes can be pushed to other processes running on other machines as easily as they can be pushed to other Views in your shell.

Hope this helps; I can offer more clarification if required.

Leave a Comment