Skip to content
Antony Male edited this page Feb 11, 2015 · 15 revisions

The bootstrapper is responsible for bootstrapping your application. It configures the IoC container, creates a new instance of your root ViewModel and displays it using the WindowManager. It also provides various other functions, described below.

The bootstrapper comes in two flavoures: BootstrapperBase<TRootViewModel>, which requires you to configure the IoC container yourself, and Bootstrapper<TRootViewModel>, which uses Stylet's built-in IoC container, StyletIoC.

Example bootstrapper, using StyletIoC:

class Bootstrapper : Bootstrapper<MyRootViewModel>
{
   protected override void OnStart()
   {
      // This is called just after the application is started, but before the IoC container is set up.
      // Set up things like logging, this.Assemblies, etc
   }

   protected override void ConfigureIoC(IStyletIoCBuilder builder)
   {
      // Bind your own types. Concrete types are automatically self-bound.
      builder.Bind<IMyInterface>().To<MyType>();
   }

   protected override void Configure()
   {
      // This is called after Stylet has created the IoC container, so this.Container exists, but before the
      // Root ViewModel is launched.
      // Configure your services, etc, in here
   }

   protected override void OnLaunch()
   {
      // This is called just after the root ViewModel has been launched
      // Something like a version check that displays a dialog might be launched from here
   }

   protected override void OnExit(ExitEventArgs e)
   {
      // Called on Application.Exit
   }

   protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e)
   {
      // Called on Application.DispatcherUnhandledException
   }
}

Using a Custom IoC Container

Using another IoC container with Stylet is easy. I've included bootstrappers for a number of popular IoC containers in the Bootstrappers project. These are all unit-tested but not battle-tested: feel free to customize them.

Note that the Stylet nuget package / dll don't include these, as it would add unnecessary dependencies. Similarly, I don't publish IoC container-specific packages, as that's a waste of effort. Just copy the bootstrapper you want from the above link into your project.

If you want to write your own bootstrapper for another IoC container, that's easy too. Take a look at the bootstrappers above to see what you need to do.

Clone this wiki locally