Introduction
This article shows how to create a bootstrapper for your Prism application using WPF with a short explanation of what a bootstrapper is, such as definition, how it works and so on. Let's start with the definition of what the bootstapper is, how it works and its relation with Prism.
Bootstrapper
A bootstrapper is a class responsible for the initialization of an application built using the Prism Library. By using a bootstrapper, you have more control of how the Prism Library components are wired to your application.
The Prism Library includes a default abstract Bootstrapper base class that can be specialized for use with any container. Many of the methods on the bootstrapper classes are virtual methods. You can override these methods as appropriate in your own custom bootstrapper implementation.
Basic stages of the bootstrapping process
- Createa LoggerFacade.
- Createand Configure a module catalog
- Createand Configure the container
- Configuredefault region adapter mappings
- Configuredefault region behaviors.
- Registerframework exception types.
- Createthe Shells.
- Initializethe shell
- Initializethe module
Creating and Configuring the Module Catalog
If you are building a module application then you will need to create and configure a module catalog. Prism uses a concrete IModuleCatalog instance to track what modules are available to the application, which modules may need to be downloaded, and where the modules reside.
The Bootstrapper provides a protected ModuleCatalog property to reference the catalog as well as a base implementation of the virtual CreateModuleCatalog method. The base implementation returns a new ModuleCatalog; however, this method can be overridden to provide a different IModuleCatalog instance instead, as shown in the following code from the QuickStartBootstrapper in the Modularity with MEF for Silverlight QuickStart.
C# Copy Code
protected override IModuleCatalog CreateModuleCatalog()
{
// When using MEF, the existing Prism ModuleCatalog is still
// the place to configure modules via configuration files.
return ModuleCatalog.CreateFromXaml(new Uri(
"/ModularityWithMef.Silverlight;component/ModulesCatalog.xaml",
UriKind.Relative));
}
In both the UnityBootstrapper and MefBootstrapper classes, the Run method calls the CreateModuleCatalog method and then sets the class's ModuleCatalog property using the returned value. If you override this method then it is not necessary to call the base class's implementation because you will replace the provided functionality.
Creating and Configuring the Container
Containers play a key role in an application created with the Prism Library. Both the Prism Library and the applications built on top of it depend on a container for injecting required dependencies and services. During the container configuration phase, several core services are registered. In addition to these core services, you may have application-specific services that provide additional functionality as it relates to composition.
Note
Dependency Injection
Applications built with the Prism Library rely on dependency injection provided by a container. The library provides assemblies that work with the Unity Application Block (Unity) or Managed Extensibility Framework (MEF), and it allows you to use other dependency injection containers. Part of the bootstrapping process is to configure this container and register types with the container.
The Prism Library includes the UnityBootstrapper and MefBootstrapper classes, that implement most of the functionality necessary to use either Unity or MEF as the dependency injection container in your application. In addition to the stages shown in the previous illustration, each bootstrapper adds some steps specific to its container.
Creating and Configuring the Container
Containers play a key role in an application created with the Prism Library. Both the Prism Library and the applications built on top of it depend on a container for injecting required dependencies and services. During the container configuration phase, several core services are registered. In addition to these core services, you may have application-specific services that provide additional functionality as it relates to composition.
Core Services
The following details lists the core non-application specific services in the Prism Library.
- IModuleManager: Defines the interface for the service that will retrieve and initialize the application's modules.
- IModuleCatalog: Contains the metadata about the modules in the application. The Prism Library provides several different catalogs.
- IModuleInitializer: Initializes the modules.
- IRegionManager: Registers and retrieves regions, that are visual containers for layout.
- IEventAggregator: A collection of events that are loosely coupled between the publisher and the subscriber.
- ILoggerFacade: A wrapper for a logging mechanism, so you can choose your own logging mechanism. The Stock Trader Reference Implementation (StockTrader RI) uses the Enterprise Library Logging Application Block,via the EnterpriseLibraryLoggerAdapter class, as an example of how you can use your own logger. The logging service is registered with the container by the bootstrapper's Run method, using the value returned by the CreateLogger method. Registering another logger with the container will not work; instead override the CreateLogger method on the bootstrapper.
- IServiceLocator: Allows the Prism Library to access the container. If you want to customize or extend the library, this may be useful.
Application-Specific Services
The following details lists the application-specific services used in the Stock Trader RI. This can be used as an example to understand the types of services your application may provide.
- IMarketFeedService: Provides real-time (mocked) market data. The PositionSummaryPresentationModel updates the position screen based on notifications it receives from this service.
- IMarketHistoryService: Provides historical market data used for displaying the trendline for the selected fund.
- IAccountPositionService: Provides the list of funds in the portfolio.
- IOrdersService: Persists submitted buy/sell orders.
- INewsFeedService: Provides a list of news items for the selected fund.
- IWatchListService: Handles when new watch items are added to the watch list.
There are two Bootstrapper-derived classes available in Prism, the UnityBootstrapper and the MefBootstrapper. Creating and configuring the various containers involve similar concepts that are implemented differently.
Creating and Configuring the Container in the UnityBootstrapper
The UnityBootstrapper class's CreateContainer method simply creates and returns a new instance of a UnityContainer. In most cases, you will not need to change this functionality; however, the method is virtual, thereby allowing that flexibility.
After the container is created, it probably needs to be configured for your application. The ConfigureContainer implementation in the UnityBootstrapper registers a number of core Prism services by default, as shown here.
An example of this is when a module registers module-level services in its Initialize method.
protected virtual void ConfigureContainer()
{
...
if (useDefaultConfiguration)
{
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);
Join the conversation! Your thoughts help the community grow.