View this website on your mobile or tablet | Follow me on twitter @SyntaxArt

Archive for November, 2012

How to create the Prism Bootstrapper

First of all the Prism Bootstrapper is the starting point of the application.  Here you will instantiate the objects which are required for Prism to execute and support your application.  Once you have implemented your bootstrapper, the Run() method will be called and the bootstrapper will begin executing.  An example of the following discussion will be provided for you to download.  You can either download the Bootstrapper class or the full project below.

You will need Visual Studio 2010 or the Visual Studio Express development environment to implemented the following.

To use Prism you will need to create a reference to the following libraries:

  • Microsft.Practices.Prism
  • Microsft.Practices.Prism.Interactivity
  • Microsft.Practices.Prism.UnityExtensions
  • Microsft.Practices.ServiceLocation

These DLL libraries can be found in the Prism download at Patterns & Practices – Prism V 4.1, alternatively you can use Manage NuGet Packages directly in Visual Studio to acquire the Prism package.

The AppBootstrapper.cs class object

Create a class object called AppBootstrapper and derive from UnityBootstrapper, this will enable you to override the bootstrapper methods and inherit the correct properties and methods.

The full class object which is discuss below can be downloaded here: AppBootstrapper.cs.  You can download the full project example here Prism Bootstrapper Solution.  There are some extra features developed in the project which is purely to get a working example of Prism and to display a Shell.  You can ignore these extras and just focus on creating your bootstrapper and displaying your Shell.

public class AppBootstrapper : UnityBootstrapper

Once you have created your AppBootstrapper object you can now override the CreateModuleCatalog() method. This method will configure our catalog of modules for the application. For the purpose of modularization we will be creating modules for each section of our application. For now just return new ConfigurationModuleCatalog(), this will read the application modules configuration from the application App.config.

protected override IModuleCatalog CreateModuleCatalog()
    return new ConfigurationModuleCatalog();

It is now important to register our types with our IOC (Inversion of Control) container.  This will allow object coupling at runtime by an assembler object whereby the object is not known at compile time.  If we override the ConfigureContainer() method here we can register our types.  To register a type first you must pass in the interface and then the type which inherits the interface.  I will not explain at this stage why it is important to register objects and their interfaces at this point in this article. This will be covered in detail later.

protected override void ConfigureContainer()

RegisterTypeIfMissing(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), true);
RegisterTypeIfMissing(typeof(IModuleInitializer), typeof(ModuleInitializer), true);
RegisterTypeIfMissing(typeof(IModuleManager), typeof(ModuleManager), true);
RegisterTypeIfMissing(typeof(RegionAdapterMappings), typeof(RegionAdapterMappings), true);
RegisterTypeIfMissing(typeof(IRegionManager), typeof(RegionManager), true);
RegisterTypeIfMissing(typeof(IEventAggregator), typeof(EventAggregator), true);
RegisterTypeIfMissing(typeof(IRegionViewRegistry), typeof(RegionViewRegistry), true);
RegisterTypeIfMissing(typeof(IRegionBehaviorFactory), typeof(RegionBehaviorFactory), true);

There are two other methods which can be overridden in the AppBootstrapper class these are; ConfigureRegionAdapterMappings() and ConfigureDefaultRegionBehaviors().  These two functions are very useful when adding region context to UIElements.  Again this is out of scope for this article.

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()

protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()

The next method is very important, this method will create our shell object. The shell object can be seen as the most outer-container or our visual root item of the application; i.e the shell. To enable us to create the shell we first need to instantiate an object to the RegionManager. The region manager allows us to register our views to a particular region within our region manager. Once a view has been registered to that region this is where it will be visible when activated.

protected override DependencyObject CreateShell()
IRegionManager regionManager = Container.Resolve();
regionManager.RegisterViewWithRegion(RegionNames.ShellRegion, typeof(MainWindowView));
regionManager.RegisterViewWithRegion(RegionNames.ClientRegion, typeof(DockingLayoutView));

return ServiceLocator.Current.GetInstance();

In order for your regions to work, you will need to register your regions with specific UIElement controls.  Prism has built in region adapters which can be attached to UIElement ContentControl.  To attach a prism region to the control you will first need to create a view.  Create a ShellView.xaml file and declare the following namespace;  “xmlns:region=”clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism”.  Once you have declared the namespace you are then able to attach the region to the content control. This will allow you to host views within this region.  See below for the following code.  You must give you region a name, this is used to reference the region in the region manager.  In the code below a static class has been created to hold the region name.

ShellView.xaml code
<Window x:Class="Orcas.Shell"

<Grid x:Name="AppRoot">
<ColumnDefinition Width="*" />
<RowDefinition Height="*" />

<ContentControl region:RegionManager.RegionName="{x:Static core:RegionNames.ShellRegion}" />

Our final function will initialize our Shell view in the application and display it. In the create shell function we resolve our RegionManager and register our MainWindowView with the ShellRegion. Now we take our Shell parameter which has been instantiated in our base object (UnityBootstrapper) and we cast this over to the application MainWindow object to display.

protected override void InitializeShell()
Application.Current.MainWindow = (Window)Shell;