1

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?

Animesh
  • 4,926
  • 14
  • 68
  • 110
Kit
  • 20,354
  • 4
  • 60
  • 103
  • Which technology are you targeting WPF, Silverlight, or other? – Eugene S. Aug 23 '11 at 20:58
  • Silverlight at the moment, but I'd be interested in a technology agnostic pattern as well. Perhaps this should be a separate question. – Kit Aug 23 '11 at 21:41

1 Answers1

1

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.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • I'm good with that part, but what about an eventing service layer. For example, the out-of-stock notification originates from a service, not a UI via binding. Are you suggesting adding an OutOfStockCommand to the view model? – Kit Aug 30 '11 at 13:26
  • Your service would implement an interface such as IOutOfStockEvent with a C# event implemented. Your ViewModel would have an instance of IOutOfStockEvent it could access and could subscribe to the event how you would normally in C#. When the event fires, you would do whatever work you needed to do in C#. You could use dependency injection to pass in the IOutOfStockEvent to your ViewModel as well. – jonathanpeppers Aug 30 '11 at 22:10
  • 1
    Here is a link in my open source project samples that demonstrates services: http://nomvvm.codeplex.com/SourceControl/changeset/view/82732#1639419 It doesn't use an event, but you see where you could easily have an event on `_mbService` and subscribe to it. There are examples of using a couple services within that ViewModel. – jonathanpeppers Aug 30 '11 at 22:16
  • Ok, makes sense. Thanks for the link. NoMvvm looks pretty awesome; even with code gen, MVVM can be a bit tedious. – Kit Aug 31 '11 at 14:10