Prism vs Caliburn.Micro

After several years of work with Prism (https://github.com/PrismLibrary/Prism), I introduced Caliburn.Micro framework (https://github.com/Caliburn-Micro/Caliburn.Micro) for building MVVM applications.

In this post I’m going to explore the Caliburn.Micro features over Prism features.
What are the advantages and disadvantages of each framework, and when using Caliburn.Micro micro is better than using Prism.

Features:

Binding:

Caliburn.Micro enables us to bind the ViewModel properties to the View, based on conventions. When Prism using the ordinary WPF binding mechanism.

The best example to describe this feature is Command binding between View and ViewModel, Caliburn.Micro enables us to bind the Commands to the ViewModel handlers without using the ICommand implementation.

For example: we can write the following code:

View:

<TextBox x:Name="Comment" />
<Button x:Name="CheckIn"  Content="Check in"/>

View Model:

private string _comment;

public string Comment

   get { return _comment; }
   set
   {
       _comment = value;
       NotifyOfPropertyChange(nameof(Comment));
       NotifyOfPropertyChange(nameof(CanCheckIn));
   }


ublic bool CanCheckIn { get { return !string.IsNullOrWhiteSpace(Comment); }  }

public void CheckIn()
{
   
}

In this example, the names of View controls must be identical to the ViewModel properties names.

View first or View Model First:

In theory the ViewModel not knowing what the view is look like. And we can create multiple views for a single ViewModel.

Prism pushes us toward the “View First” construction, in which the View is Instantiated first and then the ViewModel. In other hand, Caliburn.Micro prefers ViewModel- First.
Both framework allows the “View First” or the ViewModel- First favors.

ViewModelLocator (Prism):

Because Prism is “View First” favors, one of the new Prism 5.0 feature is the ViewModelLocator. This feature locate and instantiate the view model into the DataContext of the View based on conventions.
In order to apply this feature we need to define the prism:ViewModelLocator.AutoWireViewModel=”True” attached property into our view.

ViewLocator (Caliburn.Micro):

Cliburn.Micro provides by default a strategy to decide which View to use for a given ViewModel.
ViewLocator class is responsible for instantiate the relevant View for a given ViewModel.

Conductor (Caliburn.Micro):

A Conductor is simply a ViewModel which owns another ViewModel, and knows how to manage its lifecycle. Imagine that we have a master details application, and we want to display different views depend on the data we clicked on.
Conductor is the way to implement that behavior.

In this case MainViewModel is the conductor of the details view models.
Whenever item the user selects, the Conductor is going to activate and deactivate the relevant ViewModel.

Navigating between Views in same Region (Prism):

If we go back to our master details example above, Prism let us to display different views in the same region by the RegionManager. Prism regions are essentially named placeholders within which views can be displayed, In this case we don’t need the MainViewModel to own other view models; we just open and close different views in the same region.

Performance perspective of Conductor and Region manager:  

It is often more efficient to update an existing view instead of replace it with a new instance of the same view. Bothe framework provides mechanism to navigate between views.

When we use the Conductor pattern of Caliburn.Micro, we can re-use the instantiation of the ViewModel, by resolving the instance from the container.
And if we want to reuse the instantiation of the View, we also need to register the view as singleton in the container, The ViewLocator try to resolve the view from the container first, and if does not resolved, the View instantiate again.
Prism provides the ability to hide and show already instantiated views and view models.

The following link: https://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx   describes how to navigate between views with Prism in efficient way.

Event Aggregator:

Both frameworks enable to publish and subscribe events in a loosely based fashion.

Each framework implements EventAggregator mechanism differently.For example: Prism force us to inherit from CompositeWpfEvent<> in order to create an event.
Publishers use the Publish method; subscribers use the Subscribe method of the CompositeWpfEvent object.Prism also let the subscribers to filter the event before the registered handler is called.

With Caliburn.Micro every object can be published by EventAggregator as event. IEventAggregator interface of Caliburn.Micro provides us Publish and Subscribe methods.

Subscribers need to implement the Handle method of IHandle interface and call the_eventAggregator.Subscribe(this) in the constructor.

IOC Container support:

Both frameworks provide the ability to use any kind of IOC Container.

Caliburn.Micro provides built in Dependency Injection container called SimpleContainer.

If we want to use other IOC container, like Castle Windsor, Unity or any other container, we can register required services by override SelectAssemblies(), PrepareApplication() and Configure() methods of BootstrapperBase object.

UnityBootstrapper, and MefBootstrapper extension packages of Prism, provides the basic bootstrapping sequences that register most of Prism services we need.

It is very simple to use those extensions.

Modularity:

We all know the benefits of building “modular” application instead “monolithic” application. By create “modular” application we can easily re use modules, the application become easily more flexible, and extendible. But on the other hand building “modular” application requires us to invest more time and resources to achieve those benefits.

Prism provides best support for modular application development. Following link describe this topic very good:

https://msdn.microsoft.com/en-us/library/gg405479(v=pandp.40).aspx

Good sample for create “modular” application by Caliburn.Micro we can find in the following link: https://github.com/lukebuehler/Caliburn-Composite-Prototype

Summary:

Bothe framework provides set of tools to create MVVM applications.

Caliburn.Micro is lightweight compared to Prism.
Caliburn.Micro provides the benefits of not write binding expressions in XAML, but in complex cases we must write XAML binding expressions.

Prism provide better support for create “Modular” applications.
Prism allows managing and maintaining large-scale of enterprise projects easier with the helping tools Prism provides to build modular applications.
If we want to choose between the two frameworks, I think it depend what kind of application we going to create. For big-scale “modular” applications the best choice is Prism, and for small applications (one solution) Caliburn.Micro is the better choice.

Advertisements

About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s