Introduction
This blog will explain how to implement Castle Windsor Dependency injection in MVC.
Steps to implement CastleWinsdor
- Go to Project, right-click on "References", select "Manage NuGet Packages"
- Search for "Castle.Windsor" in the search bar.
- Install it.
- Go to App_Start folder.
- Create a class called "ContainerBootstrapper".
It should be like below.
- using System;
- using Castle.Windsor;
- using Castle.Windsor.Installer;
- namespace projectname.App_Start
- {
- public class ContainerBootstrapper : IContainerAccessor, IDisposable
- {
-
- readonly IWindsorContainer container;
-
- ContainerBootstrapper(IWindsorContainer container)
- {
- this.container = container;
- }
-
- public IWindsorContainer Container
- {
- get { return container; }
- }
-
- public static ContainerBootstrapper Bootstrap()
- {
- var container = new WindsorContainer().
- Install(FromAssembly.This());
- return new ContainerBootstrapper(container);
- }
-
- public void Dispose()
- {
- Container.Dispose();
- }
-
- }
- }
Create one more class called "WindsorActivator".
- using System;
- using WebActivatorEx;
-
- [assembly: PreApplicationStartMethod(typeof(projectname.App_Start.WindsorActivator), "PreStart")]
- [assembly: ApplicationShutdownMethodAttribute(typeof(projectname.App_Start.WindsorActivator), "Shutdown")]
-
- namespace projectname.App_Start
- {
- public class WindsorActivator
- {
- static ContainerBootstrapper bootstrapper;
-
- public static void PreStart()
- {
- bootstrapper = ContainerBootstrapper.Bootstrap();
- }
-
- public static void Shutdown()
- {
- if (bootstrapper != null)
- bootstrapper.Dispose();
- }
- }
- }
Create a folder named "Plumbing".
Create a class called "WindsorControllerFactory".
- using System;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- using Castle.Windsor;
-
-
- namespace Projectname.Plumbing
- {
- public class WindsorControllerFactory : DefaultControllerFactory
- {
- readonly IWindsorContainer container;
-
- public WindsorControllerFactory(IWindsorContainer container)
- {
- this.container = container;
- }
-
- protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
- {
- if (controllerType != null && container.Kernel.HasComponent(controllerType))
- return (IController)container.Resolve(controllerType);
-
- return base.GetControllerInstance(requestContext, controllerType);
- }
-
- public override void ReleaseController(IController controller)
- {
- container.Release(controller);
- }
- }
- }
Create a folder named "Installers" and in it, create a class called "ControllersInstaller".
- using System.Web.Mvc;
- using Castle.MicroKernel.Registration;
- using Castle.MicroKernel.SubSystems.Configuration;
- using Castle.Windsor;
- using CYBG.WebProject.Plumbing;
-
- namespace Projectname.Installers
- {
- public class ControllersInstaller : IWindsorInstaller
- {
- public void Install(IWindsorContainer container, IConfigurationStore store)
- {
- container.Register(
- Classes.
- FromThisAssembly().
- BasedOn<IController>().
- If(c => c.Name.EndsWith("Controller")).
- LifestyleTransient());
-
- ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
- }
- }
- }
Create one more class called "ServiceInstaller" inside the Installers folder. Here, you can write and configure interfaces and their implemented classes
- using Castle.MicroKernel.Registration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Projectname.Installers
- {
- public class ServiceInstaller : IWindsorInstaller
- {
- public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
- {
- container.Register(
- Component
- .For<sampleinterface>()
- .ImplementedBy<sampleclass>()
- .LifestyleSingleton());
- }
- }
- }
Finally, we are alomost done. So, we need to call in controller.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace Projectname.Controllers
- {
- public class TestController : Controller
- {
-
- private ISampleservice _BusinessService;
- public TestController(ISampleService BusinessService)
- {
- this._BusinessService = BusinessService;
- }
-
- public ActionResult Index()
- {
- var data = _BusinessService.GetAll();
- return View("Index",data);
-
- }
- }
- }