How To Implement Castle Windsor Dependency Injection In MVC

Introduction

This blog will explain how to implement Castle Windsor Dependency injection in MVC.

Steps to implement CastleWinsdor

  1. Go to Project, right-click on "References", select "Manage NuGet Packages"
  2. Search for "Castle.Windsor" in the search bar.
  3. Install it.
  4. Go to App_Start folder.
  5. Create a class called "ContainerBootstrapper".

It should be like below.

  1. using System;  
  2. using Castle.Windsor;  
  3. using Castle.Windsor.Installer;  
  4. namespace projectname.App_Start  
  5. {  
  6.     public class ContainerBootstrapper : IContainerAccessor, IDisposable  
  7.     {  
  8.   
  9.         readonly IWindsorContainer container;  
  10.   
  11.         ContainerBootstrapper(IWindsorContainer container)  
  12.         {  
  13.             this.container = container;  
  14.         }  
  15.   
  16.         public IWindsorContainer Container  
  17.         {  
  18.             get { return container; }  
  19.         }  
  20.   
  21.         public static ContainerBootstrapper Bootstrap()  
  22.         {  
  23.             var container = new WindsorContainer().  
  24.                 Install(FromAssembly.This());  
  25.             return new ContainerBootstrapper(container);  
  26.         }  
  27.   
  28.         public void Dispose()  
  29.         {  
  30.             Container.Dispose();  
  31.         }  
  32.   
  33.     }  
  34. }  

Create one more class called "WindsorActivator".

  1. using System;  
  2. using WebActivatorEx;  
  3.   
  4. [assembly: PreApplicationStartMethod(typeof(projectname.App_Start.WindsorActivator), "PreStart")]  
  5. [assembly: ApplicationShutdownMethodAttribute(typeof(projectname.App_Start.WindsorActivator), "Shutdown")]  
  6.   
  7. namespace projectname.App_Start  
  8. {  
  9.     public class WindsorActivator  
  10.     {  
  11.         static ContainerBootstrapper bootstrapper;  
  12.   
  13.         public static void PreStart()  
  14.         {  
  15.             bootstrapper = ContainerBootstrapper.Bootstrap();  
  16.         }  
  17.   
  18.         public static void Shutdown()  
  19.         {  
  20.             if (bootstrapper != null)  
  21.                 bootstrapper.Dispose();  
  22.         }  
  23.     }  
  24. }  

Create a folder named "Plumbing".

Create a class called "WindsorControllerFactory".

  1. using System;  
  2. using System.Web;  
  3. using System.Web.Mvc;  
  4. using System.Web.Routing;  
  5. using Castle.Windsor;  
  6.   
  7.   
  8. namespace Projectname.Plumbing  
  9. {  
  10.     public class WindsorControllerFactory : DefaultControllerFactory  
  11.     {  
  12.         readonly IWindsorContainer container;  
  13.   
  14.         public WindsorControllerFactory(IWindsorContainer container)  
  15.         {  
  16.             this.container = container;  
  17.         }  
  18.   
  19.         protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)  
  20.         {  
  21.             if (controllerType != null && container.Kernel.HasComponent(controllerType))  
  22.                 return (IController)container.Resolve(controllerType);  
  23.   
  24.             return base.GetControllerInstance(requestContext, controllerType);  
  25.         }  
  26.   
  27.         public override void ReleaseController(IController controller)  
  28.         {  
  29.             container.Release(controller);  
  30.         }  
  31.     }  
  32. }  

Create a folder named "Installers" and in it, create a class called "ControllersInstaller".

  1. using System.Web.Mvc;  
  2. using Castle.MicroKernel.Registration;  
  3. using Castle.MicroKernel.SubSystems.Configuration;  
  4. using Castle.Windsor;  
  5. using CYBG.WebProject.Plumbing;  
  6.   
  7. namespace Projectname.Installers  
  8. {  
  9.     public class ControllersInstaller : IWindsorInstaller  
  10.     {  
  11.         public void Install(IWindsorContainer container, IConfigurationStore store)  
  12.         {  
  13.             container.Register(  
  14.                 Classes.  
  15.                     FromThisAssembly().  
  16.                     BasedOn<IController>().  
  17.                     If(c => c.Name.EndsWith("Controller")).  
  18.                     LifestyleTransient());  
  19.   
  20.             ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));  
  21.         }  
  22.     }  
  23. }  

Create one more class called "ServiceInstaller" inside the Installers folder. Here, you can write and configure interfaces and their implemented classes

  1. using Castle.MicroKernel.Registration;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace Projectname.Installers  
  7. {  
  8.     public class ServiceInstaller : IWindsorInstaller  
  9.     {  
  10.         public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)  
  11.         {  
  12.             container.Register(  
  13.                  Component  
  14.                  .For<sampleinterface>()  
  15.                  .ImplementedBy<sampleclass>()  
  16.                  .LifestyleSingleton());  
  17.         }  
  18.     }  
  19. }  

Finally, we are alomost done. So, we need to call in controller.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace Projectname.Controllers  
  8. {  
  9.     public class TestController : Controller  
  10.     {  
  11.   
  12.         private  ISampleservice _BusinessService;  
  13.         public TestController(ISampleService BusinessService)  
  14.         {  
  15.             this._BusinessService = BusinessService;  
  16.         }  
  17.   
  18.         public ActionResult Index()  
  19.         {  
  20.             var data = _BusinessService.GetAll();  
  21.              return View("Index",data);  
  22.              
  23.         }  
  24. }  
  25. }  
Ebook Download
View all
Learn
View all