Skip to content

Using Bailey.Web.Mvc

Tieson Trowbridge edited this page Jun 11, 2015 · 3 revisions

Using Bailey.Web.Mvc is fairly straight-forward. Let's see how:

  1. Open your site's Global.asax.cs file.

  2. Add the Castle.Windsor.IContainerAccessor interface to the root class. In the current (MVC5) template, this is named MvcApplication.

  3. Implement the interface. I typically implement it like so:

    private static IWindsorContainer __container;
    
    public IWindsorContainer Container
    {
        get { return __container; }
    }
    
  4. Add a BootstrapContainer() method to the MvcApplication class. This lets us encapsulate the DI methods into one location in our class:

    private void BootstrapContainer()
    {
    }
    
  5. In BootstrapContainer(), add the relevant code to instantiate the DI container:

    private void BootstrapContainer()
    {
        __container = new WindsorContainer();
    }
    

    If you're using installers (and you should be!), tweak the line above slightly:

    private void BootstrapContainer()
    {
        __container = new WindsorContainer().Install(FromAssembly.This());
    }
    
  6. Assign the dependency resolver and controller factory:

    private void BootstrapContainer()
    {
        __container = new WindsorContainer().Install(FromAssembly.This());
    
        System.Web.Mvc.DependencyResolver.SetResolver(new WindsorDependencyResolver(__container));
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(__container.Kernel));
    }
    
  7. Lastly, call our BootstrapContainer() method in Application_Start():

    protected void Application_Start()
    {
        // ...
    
        BootstrapContainer();
    
        // ...
    }
    

At this point, your application is setup to use the Bailey.Web.Mvc components. Good job!

Clone this wiki locally