Caliburn Micro and Simple Injector IOC in WPF MVVM: Part I BootStrapper

This is the first part of an article series on the basics of using Caliburn micro for the MVVM framework in WPF. In this article we will see the benefits of using the MVVM framework and how it can ease your life when working with the MVVM design, especially with WPF. For the series I've chosen Caliburn Micro because this is a lightweight, powerful, easy to use and highly recommended framework with a very good technical documentation. Second we'll talk about the SimpleInjector for IOC (Inversion of control) because this is reported to be the fastest and easiest to use IOC container framework. The IOC container is required with Caliburn micro in the bootstrapper (Entry point) to register all the view model types and other types so that they can be resolved at run times. The IOC keeps our application fully decoupled and this is what MVVM is for. You'll see the true MVVM taste here with fully decoupled views and view models in upcoming posts.

So let's get started with the first step writing the Bootstrapper for the WPF application.

Application Bootstrapper

The bootstrapper is required to register and configure all the runtime and startup components. This will be your entry point of your WPF application. And rather than directly adding the reference to your startup window you'll be registering the bootstrapper in App.XAML.

Writing the bootstrapper.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using DataGridSample;
using SimpleInjector;
 
namespace CaliburnMicroMvvmWpfDemo
{
    internal class AppBootstrapper : Bootstrapper<MainWindowView>
    {
        
/// <summary>
        /// The global container.
        /// </summary>
        public static readonly Container ContainerInstance = newContainer();
 
        
protected override void Configure()
        {
            ContainerInstance.Register<
IWindowManagerWindowManager>();
            ContainerInstance.RegisterSingle<IEventAggregator,EventAggregator>();
 
            ContainerInstance.Verify();
        }
 
        
protected override IEnumerable<object> GetAllInstances(Type service)
        {
            
return ContainerInstance.GetAllInstances(service);
        }
 
        
protected override object GetInstance(System.Type service, stringkey)
        {
            
return ContainerInstance.GetInstance(service);
        }

         protected override void BuildUp(object instance)
        {
            ContainerInstance.InjectProperties(instance);
        }
    }
}

In the code above, in order to create a bootstrapper we just need to tell the builtin Bootstrapper<> the type of startup window view model and inherit the class in our AppBootstrapper class. Now we need to override a few builtin methods of Bootstrapper<> in order to complete the configuration. Register the interfaces of IWindowManager and IEventAggregator (optional and for later use) in the configure method. Just use the code above to write your initial bootstrapper though you need to add more lines of code in the Configure method to register all your other view model types later.

Updating the App.XAML

To add the entry of bootstrapper change the App.xaml file to look something like the following:

<Application x:Class="CaliburnMicroMvvmWpfDemo.App"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Boot ="clr-namespace:CaliburnMicroMvvmWpfDemo">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Boot:AppBootstrapper x:Key = "bootStrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</
Application>

Once you have changed the setting above, your application is ready to launch. You have successfully created the entry point for the application. Now to have your Global event handler Caliburn Micro provide overrides for internal exception handlers that you had in App.xaml.cs, the following events can be added in the AppBootstrapper class.

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
    // do custom code here which you want to run on startup
    base.OnStartup(sender, e);
}
 
protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    // Write you custom code for handling Global unhandled excpetion of Dispatcher or UI thread.
    base.OnUnhandledException(sender, e);
}

Here in this article I tried to cover the starting step of adding the bootstrapper for caliburn micro. Also if you can see we have create our MainWindowViewModel class first but haven't written anything in it; neither have we any view here. In the next article we'll see creation of a main window view model and the basic binding and other MVVM stuff.

Hope you enjoyed it. Please leave a comment or suggestion.

Up Next
    Ebook Download
    View all
    Learn
    View all