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. readonly IWindsorContainer container;
  9. ContainerBootstrapper(IWindsorContainer container)
  10. {
  11. this.container = container;
  12. }
  13. public IWindsorContainer Container
  14. {
  15. get { return container; }
  16. }
  17. public static ContainerBootstrapper Bootstrap()
  18. {
  19. var container = new WindsorContainer().
  20. Install(FromAssembly.This());
  21. return new ContainerBootstrapper(container);
  22. }
  23. public void Dispose()
  24. {
  25. Container.Dispose();
  26. }
  27. }
  28. }

Create one more class called "WindsorActivator".

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

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. namespace Projectname.Plumbing
  7. {
  8. public class WindsorControllerFactory : DefaultControllerFactory
  9. {
  10. readonly IWindsorContainer container;
  11. public WindsorControllerFactory(IWindsorContainer container)
  12. {
  13. this.container = container;
  14. }
  15. protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
  16. {
  17. if (controllerType != null && container.Kernel.HasComponent(controllerType))
  18. return (IController)container.Resolve(controllerType);
  19. return base.GetControllerInstance(requestContext, controllerType);
  20. }
  21. public override void ReleaseController(IController controller)
  22. {
  23. container.Release(controller);
  24. }
  25. }
  26. }

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. namespace Projectname.Installers
  7. {
  8. public class ControllersInstaller : IWindsorInstaller
  9. {
  10. public void Install(IWindsorContainer container, IConfigurationStore store)
  11. {
  12. container.Register(
  13. Classes.
  14. FromThisAssembly().
  15. BasedOn<IController>().
  16. If(c => c.Name.EndsWith("Controller")).
  17. LifestyleTransient());
  18. ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
  19. }
  20. }
  21. }

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. namespace Projectname.Controllers
  7. {
  8. public class TestController : Controller
  9. {
  10. private ISampleservice _BusinessService;
  11. public TestController(ISampleService BusinessService)
  12. {
  13. this._BusinessService = BusinessService;
  14. }
  15. public ActionResult Index()
  16. {
  17. var data = _BusinessService.GetAll();
  18. return View("Index",data);
  19. }
  20. }
  21. }