Monday, 12 November 2012

MVVM PRISM or ReactiveUI? Choose one or both?

This is a good topic to discuss in the MVVM world, and many developers face the dilemma of which framework to use when starting a new WPF/Silverlight project.

Honestly, I don't see ReactiveUI as a complete MVVM framework but it has bits and pieces that many other MVVM frameworks would like to have.
For instance ReactiveUI can make Developers' life easier by providing IObservable event handlers and Notifications that can run in either the UI or Background Thread through just a simple function call like ObserveOn() or ObserveOnDispatcher(). Probably you have seen already something like this in order to handle Property changes in the UI thread:
var dispatcher = System.Windows.Deployment.Current.Dispatcher;
if (dispatcher.CheckAccess())
{
    viewModel.IsVisible = true;
}
else
{
    dispatcher.BeginInvoke(
          () => { viewModel.IsVisible = true; });
}

Using Reactive you can do something like this in a ViewModel:

this.ObservableForProperty(x => x.IsVisible).ObserveOnDispatcher().Subscribe(x =>
{
     //if visible
     if(x.Value)
       //do some stuff when the IsVisible Property of the ViewModel changes...
}

or

this.WhenAny(x => x.Username,
             x => x.Email,
            (userName,email) => !string.IsNullOrEmpty(username.Value)
                          && email.Value != null
                          && email.Value.Contains("@")).Subscribe(result =>
{
     //if Username and Email are valid
     if(result .Value)
       //do some stuff...
}


ReactiveUI adds very powerful Notification capabilities to the well known Microsoft Rx framework, adding a few classes (Subject, ReactiveCommand, Observable, ReactiveObject, etc) that do make it easier to start an MVVM project. If you don't know what Rx is, what rock have you been under in the last few years? In either case if you haven't heard about Rx, then this is a good site to start learning (Rx Microsoft  tutorial).


PRISM is another very different Beast! It's a complex MVVM framework. It supports many things that other MVVM frameworks don't have. People might be led to think due to the fact that PRISM has everything, that this should be a reason to go ahead with PRISM (or Jounce), oh how mistaken you are. Let me say that MVVM Light Toolkit is a very simple MVVM framework (used it in many projects before), ideal for small projects or when the complexity of the interface doesn't require complex interactions. Projects without complex navigation requirements (navigation between panels, areas, docking, etc) are good candidates for a simple framework like MVVM Light Toolkit .

On the other hand, ReactiveUI, is very well suited to projects that have complex Enable/Disable rules, or Toolbars that have commands that need to be enabled when many different conditions are verified, or when there are many overlapped events occurring simultaneous that can be combined to disable or enable parts of the UI.

In some special cases it might pay off to mix ReactiveUI with other MVVM frameworks like PRISM. In this case, Paul Betts has a page describing how to glue ReactiveUI with other MVVM frameworks in here .

So in order to help you a bit with your next decision, I can give some examples where I would pick each MVVM framework:


Project type MVVM Framework
A CRUD Table Editor MVVM Light Toolkit
A Silverlight Project Mng Dashboard MVVM Light Toolkit
A WPF Word Processing Tool ReactiveUI
Modular application with dockable areas PRISM
WPF Stock Trader Application w/ Dashboards PRISM + ReactiveUI

Having described in which situations to use these framework, I think we can go through some of the details of both PRISM and ReactiveUI and where they overlap each other.

In essence, PRISM provides the following features:

- A Command framework implementation (DelegateCommand, CompositeCommand, etc)
- A NotificationObject ViewModel class
- View Navigation with support for Views (IRegionMemberLifetime, IConfirmNavigationRequest, INavigationAware, IConfirmNavigation, etc)
- An Application Event Bus (EventAggregator,CompositePresentationEvent)
- Dialog/User Interactions (InteractionRequest, PopupChildWindowAction, InteractiveRequest, Notification, etc)
- Application Modularity support (IModule, IModuleCatalog, etc)
- Support for many IoC containers (MEF, Unity, etc)

ReactiveUI provides similar features as well:

- A Command framework implementation (ReactiveCommand, ReactiveAsyncCommand, etc)
- An Observable ViewModel class (ReactiveObject)
- Validation engine (ReactiveValidatedObject, ValidatesViaMethodAttribute)
- View Navigation with ViewModelLocator (IViewFor<>, IRoutingState, RoutedViewHost)
- UI Thread/Synchronous event processing (through any IObservable events)
- An Application Event Bus (MessageBus)
- Support for many IoC containers (MEF, Unity, etc)
- IObservable extensions to notification filtering and chaining (Observable.Merge, Observable.Zip, Observable.CombineLatest, Observable.Scan, Observable.Throttle, Observable.Amb, etc)

So, after checking these details, you realized that there is some overlapped functions in both these frameworks. Well, you're not forced to use one framework only, I've done projects where I used PRISM and ReactiveUI together and chose the best functions of each to go with. In these situations, my ViewModels inhereted from ReactiveObject and I made used of PRISM EventAggregator (with IObservable notification extensions) instead of Reactive MessageBus, among other tweaks, but this is a discussion for another Post.

On the following posts will go through an example that compares a Maps (geographic) Silverlight application built with ReactiveUI and another built with PRISM to make it clear what differences exist between these frameworks.