I Am Not Myself


An Implementation of Castle Windsor Bootstrapper for Caliburn.Micro

This last week Jeff Schumacher and I have been working on a fairly complex set of business logic driving the entire design from the outside in with specification style unit tests. In an effort to help our tester get a head start validating the logic and assumptions in our code we promised to create a simple UI. Jeff has some WPF experience, so I suggested we do a simple WPF application and he can show me his “XAML Fu”. I have yet to get beyond playing with XAML, so having a little toy project and pairing with someone who has some experience already is a great way to get your feet wet. I also suggested we use Calibun.Micro and awesome little library Rob Eisenberg that brings convention over configuration for WPF applications to the table. So we pulled down the library and cracked open Rob’s Soup to Nuts series of tutorials.

In part two of his series, Rob describes how to override Caliburn.Micro’s default Bootstrapper with a custom implementation using the Managed Extensibility Framework. At work we use Castle Windsor, so we followed along with the tutorial injecting Castle magic at the appropriate spots.

To provide a custom bootstrapper for Caliburn.Micro, you simply inherit from the Bootstrapper<T> where T is your shell view model and then override the GetInstance, GetAllInstances and BuildUp methods providing your own implementation. With the exception of the BuildUp method, they pretty much line up perfectly with Castle Windsor’s interface allowing you to simply wrap it. The BuildUp method is used to perform property injection on an already created instance, Castle Windsor does property injection by default on resolution of your types and does not explicitly expose a build up method. This is easily overcome by adding an extension method to the container to provide that service.

public static class WindsorExtensions

        public static void BuildUp(this IWindsorContainer container, object instance)
                .Where(property =&gt; property.CanWrite &amp;&amp; property.PropertyType.IsPublic)
                .Where(property =&gt; container.Kernel.HasComponent(property.PropertyType))
                .ForEach(property =&gt; property.SetValue(instance, container.Resolve(property.PropertyType), null));

The bootstrapper then looks like this:

public class CastleBootstrapper : Bootstrapper
        private ApplicationContainer container;

        protected override void Configure()
            container = new ApplicationContainer();

        protected override object GetInstance(Type service, string key)
            if (string.IsNullOrWhiteSpace(key))
                return container.Resolve(service);

            return container.Resolve(key);

        protected override IEnumerable GetAllInstances(Type service)
            return (IEnumerable) container.ResolveAll(service);

        protected override void BuildUp(object instance)

The ApplicationContainer class is simply inherits from WindsorContainer, I like to seperate out my component registration from my bootstrapping code. It looks something like this:

public class ApplicationContainer : WindsorContainer
        public ApplicationContainer()



        private void RegisterViewModels()
                         .Where(x =&gt; x.Namespace.EndsWith("ViewModels"))
                         .Configure(x =&gt; x.LifeStyle.Is(LifestyleType.Singleton)));

This code is all that is needed to get the sample application from the tutorials to work with Castle Windsor.

2 responses to “An Implementation of Castle Windsor Bootstrapper for Caliburn.Micro

  1. Henning February 16, 2012 at 9:18 pm

    I’m wondering: why would you register the viewmodels as singleton instead of transient?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: