Subscribe RSS 2.0 cshandler feeds

Sunday, 31 March 2013

Basics of Caliburn Micro with simple injector IOC in WPF mvvm–Part I BootStrapper

This is the first part of post series for basics of using Caliburn micro for MVVM framework in WPF. In this blog post we will see the benefits of using a MVVM framework and how it can ease your life when working with MVVM design specially 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 as fastest and easy to use IOC container framework. 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 referencing of your startup window you’ll be registering the bootstrapper in App.XAML

Writing the bootstrapper -

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Caliburn.Micro;
  6. using DataGridSample;
  7. using SimpleInjector;
  8.  
  9. namespace CaliburnMicroMvvmWpfDemo
  10. {
  11.     internal class AppBootstrapper : Bootstrapper<MainWindowView>
  12.     {
  13.         /// <summary>
  14.         /// The global container.
  15.         /// </summary>
  16.         public static readonly Container ContainerInstance = new Container();
  17.  
  18.         protected override void Configure()
  19.         {
  20.             ContainerInstance.Register<IWindowManager, WindowManager>();
  21.             ContainerInstance.RegisterSingle<IEventAggregator, EventAggregator>();
  22.  
  23.             ContainerInstance.Verify();
  24.         }
  25.  
  26.         protected override IEnumerable<object> GetAllInstances(Type service)
  27.         {
  28.             return ContainerInstance.GetAllInstances(service);
  29.         }
  30.  
  31.         protected override object GetInstance(System.Type service, string key)
  32.         {
  33.             return ContainerInstance.GetInstance(service);
  34.         }
  35.  
  36.         protected override void BuildUp(object instance)
  37.         {
  38.             ContainerInstance.InjectProperties(instance);
  39.         }
  40.     }
  41. }

In the above code in order to create a bootstrapper we just need to tell the inbuilt Bootstrapper<> the type of startup window view model and inherit the class in our AppBootstrapper class. Now we need to override few inbuilt method 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 above code to write your initial bootstrapper though you need to add more lines of code in the Configure method to register your all other view model types later.

Updating the App.XAML

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

  1. <Application x:Class="CaliburnMicroMvvmWpfDemo.App"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.              xmlns:Boot ="clr-namespace:CaliburnMicroMvvmWpfDemo">
  5.     <Application.Resources>
  6.         <ResourceDictionary>
  7.             <ResourceDictionary.MergedDictionaries>
  8.                 <ResourceDictionary>
  9.                     <Boot:AppBootstrapper x:Key = "bootStrapper" />
  10.                 </ResourceDictionary>
  11.             </ResourceDictionary.MergedDictionaries>
  12.         </ResourceDictionary>
  13.     </Application.Resources>
  14. </Application>

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

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

Here in this post 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 wrote anything in it neither we have any view here. In the post we’ll see creating main window view model and basic binding and other MVVM stuff.

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

No comments:

Post a Comment