Prism vs. Caliburn.Micro

One of the .NET tracks during the Bootcamp 2012 in Barcelona offered an opportunity to use, learn and analyze Caliburn.Micro 1.3. After using PRISM 1.0 for over a year I decided to compare those 2 MVVM frameworks.

View and ViewModel communication

I was amazed by how easy it was to start using the Caliburn.Micro, given I had some support from a CM expert. The communication between the View and ViewModel is straightforward, easy to use and yet very powerful. CM wins over PRISM in this department. The ViewModel code is cleaner and I think that naming conventions are better than attributes. They require more knowledge about the framework but also encourage faster implementation.

  • Caliburn.Micro
cal:Message.Attach="[Event Click] = [Action PortfolioClicked()]"

public bool CanPortfolioClicked { ... }
public void PortfolioClicked() { ... }
  • PRISM
<Button Command="{Binding FinishCommand}"/>

this.FinishCommand = new DelegateCommand<object>(this.OnFinishCommand, this.OnCanFinishCommand);
private void OnFinishCommand(object obj) { ... }
private bool OnCanFinishCommand(object arg) { ... }

To choose or not to choose

After having such a blast I had to dig deeper. I even tried to abandon the “one framework to rule them all” way of thinking. Theoretically it is possible to combine those two Frameworks. Prism provides great support for the modular application development and offers UnityBootstrapper and MefBootstrapper. Ninject Bootstrapper is available on the Ninject forums but it’s not officially supported. So basically we could use PRISM to discover and load the modules and Caliburn.Micro for the V <-> VM interaction.
At some point I abandoned this idea since it’s just not needed:

  • PRISM requires a View-First approach
  1. The bootstrapper loads the modules.
  2. The Views are located through the ViewExport Attributes
  3. The view is instantiated and the VM is injected into the view
[ViewExport(RegionName = RegionNames.MainRegion)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class WatchListView : UserControl
{
     public WatchListView (WatchListViewModel watchListViewModel)
     {
          this.DataContext = watchListViewModel;
     }
}
  • Caliburn.Micro promotes a ViewModel-First approach
  1. The bootstrapper loads the modules
  2. IOC container finds the ViewModels that are injected into the ShellVM.
  3. Views are resolved by the ViewModelBinder

If the ViewModel-First approach is chosen, then a big part of PRISM is not required anymore and vice versa. Caliburn.Micro also supports View-First approach, which is not recommended. Even more Prism features would become redundant when using the Caliburn.Micro with View-First approach.

EventAggregation

Event aggregation is another important feature provided by both frameworks. The purpose of the Event Aggregator is to simplify event registration by channeling events from multiple objects into a single object. The usage feature is pretty much identical in both frameworks.

  • PRISM

In PRISM, an event is declared by inheriting from a CompositePresentationEvent where T is the message payload.
Subscribing has up to five parameters including whether to hold a strong or weak reference to the subscriber, which thread to call subscribers on, a filter to determine if the subscriber gets the event, and the delegate to invoke.
The defaults work for most situations.

public class FooEvent : CompositePresentationEvent<FooEventArgs> { }
myEventAggregator.GetEvent<FooEvent>().Publish(fooEventArgs);
myEventAggregator.GetEvent<FooEvent>().Subscribe(OnFoo);
  • Caliburn.Micro

Event Aggregation in Caliburn Micro is simple.  In Caliburn Micro, events are plain old CLR objects (poCo).  The event argument payload, if needed, is declared in the event as one or more simple properties. Somehow the way the event aggregation works reminds me of EvenBroker in APPcelerate.
This IHandle approach makes explicit and clear at a glance the relationship between interested subscribers and their subscribed events.
Caliburn.Micro isn’t as powerful as PRISM. Yet it prevents you from creating bugs that are very hard to pinpoint. It holds weak references in the event aggregator and executes events on the UI thread.

public class FooEvent { public string myProp; }
public class Foo : IHandle<FooEvent>
    {
        private IEventAggregator myEventAggregator;

        public Foo()
        {
            myEventAggregator.Subscribe(this);
        }

        public void Handle(FooEvent message)
        {

        }
    }
myEventAggregator.Publish<FooEvent>(new FooEvent());

Navigation

The basic navigation features are supported by both frameworks. After spending some hours analyzing the navigation features in Caliburn.Micro I have to say that PRISM offers more functionality. I wasn’t able to find support for the following features in Caliburn.Micro:

  1. State-Based Navigation – Used to display the same data with different formats and styles.
  2. View lifetime management.
  3. Navigation history
  4. Exception handling to prevent application crashes on navigation exceptions. (For example if the view is missing or view model produces an exception during the navigation)

When to use PRISM or Caliburn.Micro?

I’ve started comparing those two frameworks to find a better one. Right now I think that they’re both good just for a different type of application. If I had to develop an application with a lot of different teams working on the same application I would still pick PRISM. Support for the development of modular applications and navigation support wins here. For the smaller projects, which are being developed by a single team, I would pick Caliburn.Micro, especially if the team consists of developers that are inexperienced with both frameworks. Caliburn.Micro has everything that should be supported by a MVVM framework. On top of that less functionality also means lower complexity, resulting in lower probability of misuse or bugs.

Further Informations

In case you’d like more in depth information, here are some links that proved to be very informative:

  1. http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx#sec1 - PRISM Navigation
  2. http://devlicio.us/blogs/rob_eisenberg/archive/2010/07/06/caliburn-micro-soup-to-nuts-pt-1-configuration-actions-and-conventions.aspx - Everything you could possibly want to know about Caliburn.Micro. A series written by Rob Eisenberg.

I would like to thank Daniel Marbach for his invaluable help. He supported me on the technical part considering Caliburn.Micro and was kind enough to review the posting.

This entry was posted in .NET, UI and tagged , , , . Bookmark the permalink.

One Response to Prism vs. Caliburn.Micro

  1. Carl Cubillas says:

    Great post! I’m looking into different MVVM frameworks out there such as Caliburn.Micro, PRISM and ReactiveUI. I’m relatively new to WPF and I’m currently using MVVMLight at the moment. Thanks for posting some of their distinctions.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>