Question

Consider these two scenarios:

  • a user presses a button in a view (e.g. Fulfill Order) and we want the view to update immediately (disable the button, add a progress bar, etc.)
  • a service layer raises a business event, which ultimately must be reflected on the view (e.g. a product has become out-of-stock).

Both cases legitimately require some mechanism, X, to update the viewmodel. With MVVM, the view can do this by setting properties of the viewmodel in an event handler, through command binding, or via some other mechanism.

The service layer can do this using some mechanism, Y. For example, raising events in the business/domain model, creating commands to manipulate the viewmodel, calling methods on the viewmodel etc.

In fact, X and Y could be the same mechanism (or pattern).

What's a good one to do this, that keeps to the spirit of MVVM, but is DRY?

Was it helpful?

Solution

I think you need to pick an MVVM framework and follow the pattern for this that it supports.

In general:

  • Your button will be hooked to a FulfillOrder method on your ViewModel, via an ICommand or whichever your MVVM-framework supports
  • A "CanFulfillOrder" boolean property will be hooked up to disable your button via INotifyPropertyChanged, this can be triggered by the FulfillOrder method or the event you mention. It could also be bound to the Visibility on a progress bar.
  • Another property could provide the percentage on the progress bar and update it appropriately

A good, general-purpose MVVM framework is MVVM Light.

If you are looking for more power, and can handle more complexity, try Caliburn.

Or if you want to use dynamic and try something cutting edge, try my framework: NoMvvm.

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