Skip to content
Mike Evans-Larah By Mike Evans-Larah Software Engineer III
Using the endjin composition framework in an MVC application

As I was setting up the framework for my apprenticeship portal MVC 4 web application, I used part of endjin's core composition framework for the dependency injection, which utilises Castle Windsor.

(Here is a link to some useful videos on dependency injection and Castle Windsor that helped me to understand why they are used).

Part of the framework uses a class called WindsorContainerBootstrapper which calls a GetInstallers method which looks through all referenced assemblies and finds and returns a list of the Windsor Installers that are contained within them.

public class WindsorContainerBootstrapper : IContainerBootstrapper
  {
    public IWindsorInstaller[] GetInstallers()
    {
      return new IWindsorInstaller[1]
      {
        FromAssembly.InDirectory(new AssemblyFilter("./", (string) null))
      };
    }
  }

This was designed for console applications though. To make this work, I made a change so that it uses the HttpRuntime.BinDirectory method to correctly locate the bin folder where the assemblies are stored in a web application.

public class WindsorContainerWebBootstrapper : IContainerBootstrapper
{
    public IWindsorInstaller[] GetInstallers()
    {
        return new[] { FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "*.dll")) };
    }
}

This extra class could not be added to Endjin.Core.Composition though as HttpRuntime is dependent on System.Web, and Endjin.Core.Composition uses the .NET 4 Client Profile which does not support this.

So I added a new WindsorContainerWebBootstrapper class to the project and called it in the dependency injection configuration to initialise a new Windsor Container.

public class ContainerConfig
{
    public static void InitialiseContainer()
    {
        ApplicationServiceLocator.Initialize(new WindsorContainer(), new WindsorContainerWebBootstrapper());
        var container = ApplicationServiceLocator.Container;
        
        // MVC
        DependencyResolver.SetResolver(
            x => container.Kernel.HasComponent(x) ? container.Resolve(x) : null,
            x => container.Kernel.HasComponent(x) ? container.ResolveAll(x).Cast<object>() : new object[0]);
    }
}

This means I can now resolve my types dynamically.

(As a side note, we now have a GitHub Gist plugin installed for the blog so all the pasted code looks pretty)

@MikeLarah

Mike Evans-Larah

Software Engineer III

Mike Evans-Larah

Mike is a Software Engineer at endjin with over a decade of experience in solving business problems with technology. He has worked on a wide range of projects for clients across industries such as financial services, recruitment, and retail, with a strong focus on Azure technologies.