Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created July 17, 2012 09:17
Show Gist options
  • Select an option

  • Save martinnormark/3128275 to your computer and use it in GitHub Desktop.

Select an option

Save martinnormark/3128275 to your computer and use it in GitHub Desktop.

Revisions

  1. martinnormark revised this gist Jul 17, 2012. No changes.
  2. martinnormark revised this gist Jul 17, 2012. No changes.
  3. martinnormark revised this gist Jul 17, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@
    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- [`BusinessLogicInstaller.cs`](https://gist.github.com/3128275#file_businesslogicinstaller.cs)
    -------- [`ControllersInstaller.cs`](https://gist.github.com/3128275#file_controllersinstaller.cs)
    -------- [`IocContainer.cs`](https://gist.github.com/3128275#file_ioccontainer.cs)
    -------- [`WindsorControllerFactory.cs`](https://gist.github.com/3128275#file_windsorcontrollerfactory.cs)
    -------- [`BusinessLogicInstaller.cs`](https://gist.github.com/3128275#file_business_logic_installer.cs)
    -------- [`ControllersInstaller.cs`](https://gist.github.com/3128275#file_controllers_installer.cs)
    -------- [`IocContainer.cs`](https://gist.github.com/3128275#file_ioc_container.cs)
    -------- [`WindsorControllerFactory.cs`](https://gist.github.com/3128275#file_windsor_controller_factory.cs)

    2. Add the contents of each file in this Gist to your app. Rename namespace to fit to your application.

  4. martinnormark revised this gist Jul 17, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@
    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- BusinessLogicInstaller.cs
    -------- ControllersInstaller.cs
    -------- IocContainer.cs
    -------- WindsorControllerFactory.cs
    -------- [`BusinessLogicInstaller.cs`](https://gist.github.com/3128275#file_businesslogicinstaller.cs)
    -------- [`ControllersInstaller.cs`](https://gist.github.com/3128275#file_controllersinstaller.cs)
    -------- [`IocContainer.cs`](https://gist.github.com/3128275#file_ioccontainer.cs)
    -------- [`WindsorControllerFactory.cs`](https://gist.github.com/3128275#file_windsorcontrollerfactory.cs)

    2. Add the contents of each file in this Gist to your app. Rename namespace to fit to your application.

  5. martinnormark revised this gist Jul 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.markdown
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,6 @@

    2. Add the contents of each file in this Gist to your app. Rename namespace to fit to your application.

    3. Call `IocContainer.Setup` from `Global.asax.cs`. Example shown in this Gist.
    3. Call `IocContainer.Setup` from [`Global.asax.cs`](https://gist.github.com/3128275#file_global.asax.cs). Example shown in this Gist.

    ## That's it!
  6. martinnormark revised this gist Jul 17, 2012. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    # Castle Windsor IoC Container setup for ASP.NET MVC

    Create the following folder structure and files:
    1. Create the following folder structure and files:

    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- BusinessLogicInstaller.cs (rename as you want)
    -------- BusinessLogicInstaller.cs
    -------- ControllersInstaller.cs
    -------- IocContainer.cs
    -------- WindsorControllerFactory.cs

    2. Add the contents of each file in this Gist to your app. Rename namespace to fit to your application.

    3. Call `IocContainer.Setup` from `Global.asax.cs`. Example shown in this Gist.

    ## That's it!
  7. martinnormark revised this gist Jul 17, 2012. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@

    Create the following folder structure and files:

    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- BusinessLogicInstaller.cs (rename as you want)
    -------- ControllersInstaller.cs
    -------- IocContainer.cs
    -------- WindsorControllerFactory.cs
    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- BusinessLogicInstaller.cs (rename as you want)
    -------- ControllersInstaller.cs
    -------- IocContainer.cs
    -------- WindsorControllerFactory.cs

  8. martinnormark revised this gist Jul 17, 2012. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Castle Windsor IoC Container setup for ASP.NET MVC

    Create the following folder structure and files:

    -- MyApp.Web (The MVC project)
    ---- PresentationLogic (folder)
    ------ Container (folder)
    -------- BusinessLogicInstaller.cs (rename as you want)
    -------- ControllersInstaller.cs
    -------- IocContainer.cs
    -------- WindsorControllerFactory.cs

  9. martinnormark created this gist Jul 17, 2012.
    15 changes: 15 additions & 0 deletions BusinessLogicInstaller.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.SubSystems.Configuration;
    using Castle.Windsor;
    using MyApp.BusinessLogic.Facades;

    namespace MyApp.Web.PresentationLogic.Container
    {
    public class BusinessLogicInstaller : IWindsorInstaller
    {
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
    container.Register(Component.For<IProductBusinessFacade>().ImplementedBy<ProductBusinessFacade>());
    }
    }
    }
    17 changes: 17 additions & 0 deletions ControllersInstaller.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.SubSystems.Configuration;
    using Castle.Windsor;

    namespace MyApp.Web.PresentationLogic.Container
    {
    public class ControllersInstaller : IWindsorInstaller
    {
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
    container.Register(AllTypes.FromThisAssembly()
    .Pick().If(t => t.Name.EndsWith("Controller"))
    .Configure(configurer => configurer.Named(configurer.Implementation.Name))
    .LifestylePerWebRequest());
    }
    }
    }
    10 changes: 10 additions & 0 deletions Global.asax.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    void Application_Start(object sender, EventArgs e)
    {
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    // Add this line to Application_Start in Global.asax.cs to setup the IoC Container.
    IocContainer.Setup();
    }
    19 changes: 19 additions & 0 deletions IocContainer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    using System.Web.Mvc;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    namespace MyApp.Web.PresentationLogic.Container
    {
    public static class IocContainer
    {
    private static IWindsorContainer _container;

    public static void Setup()
    {
    _container = new WindsorContainer().Install(FromAssembly.This());

    WindsorControllerFactory controllerFactory = new WindsorControllerFactory(_container.Kernel);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }
    }
    }
    32 changes: 32 additions & 0 deletions WindsorControllerFactory.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Castle.MicroKernel;

    namespace MyApp.Web.PresentationLogic.Container
    {
    public class WindsorControllerFactory : DefaultControllerFactory
    {
    private readonly IKernel _kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
    this._kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
    _kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
    if (controllerType == null)
    {
    throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
    }
    return (IController)_kernel.Resolve(controllerType);
    }
    }
    }