Setting up Autfac with Caliburn Micro v3.2.0 and Autofac v4.8.1

I find myself frequently setting up new projects with Caliburn Micro; however, It isn’t always easy to remember the code to integrate Autofac with the bootstrapper. So, here is the template that I use when creating a new application.

public class ClientBootstrapper : BootstrapperBase
{
    private static IContainer Container;

    public ClientBootstrapper()
    {
        this.Initialize();
    }

    protected override void Configure()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<WindowManager>().AsImplementedInterfaces().SingleInstance();
        builder.RegisterType<EventAggregator>().AsImplementedInterfaces().SingleInstance();
        Container = builder.Build();
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return Container.Resolve(typeof(IEnumerable<>).MakeGenericType(service)) as IEnumerable<object>;
    }

    protected override object GetInstance(Type service, string key)
    {
        if (string.IsNullOrWhiteSpace(key))
        {
            if (Container.IsRegistered(service))
                return Container.Resolve(service);
        }
        else
        {
            if(Container.IsRegisteredWithKey(key, service))
            {
                return Container.ResolveKeyed(key, service);
            }
        }

        var msgFormat = "Could not locate any instances of contract {0}.";
        var msg = string.Format(msgFormat, key ?? service.Name);
        throw new Exception(msg);
    }

    protected override void BuildUp(object instance)
    {
        Container.InjectProperties(instance);
    }
}

Hope this helps you out! Good luck with your WPF application.

First Post

Hello World!

After multiple attempts in the past I’m looking to start blogging again.

In this iteration, I’m using an Amazon S3 bucket serving up a static site that is generated with Hugo. So far I’m pretty happy with the results.

In future iterations of the site, I’m planning to:

  • Add tagging support to the posts
  • Add the google analytics tag
  • Make some adjustments to the theme to make it even more lightweight
  • Add a landing page so this website is more than just a blog
  • Add references to the other sites where I spend my time: Github and Instagram.

Thanks for ready and see you in the next post!