Created
June 14, 2013 10:33
-
-
Save bitsprint/5780910 to your computer and use it in GitHub Desktop.
Windsor Dependency Injection Setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Web; | |
| using Web.AppStart; | |
| [assembly: WebActivator.PreApplicationStartMethod(typeof(DependencyInjection), "BootstrapContainer")] | |
| namespace Web.AppStart | |
| { | |
| using System; | |
| using System.IO; | |
| using System.Reflection; | |
| using System.Web.Mvc; | |
| using Castle.MicroKernel.Registration; | |
| using Castle.Windsor; | |
| using Castle.Windsor.Installer; | |
| public static class DependencyInjection | |
| { | |
| public static string AssemblyDirectory | |
| { | |
| get | |
| { | |
| var codeBase = Assembly.GetExecutingAssembly().CodeBase; | |
| var uri = new UriBuilder(codeBase); | |
| var path = Uri.UnescapeDataString(uri.Path); | |
| return Path.GetDirectoryName(path); | |
| } | |
| } | |
| /// <summary> | |
| /// </summary> | |
| /// <param name="container"></param> | |
| public static void ConfigureTypes(IWindsorContainer container) | |
| { | |
| container.Register( | |
| Component.For<ICacheLayer>().ImplementedBy<HttpContextCacheLayer>().LifestyleSingleton(), | |
| Component.For<IWindsorContainer>().Instance(container).Named("WindsorContainer"), | |
| Component.For<IResolver>().ImplementedBy<CastleResolver>().DependsOn(Dependency.OnComponent(typeof(IWindsorContainer), "WindsorContainer")).LifestyleSingleton(), | |
| Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)), | |
| Component.For<IFoo>().ImplementedBy<Foo>().LifestylePerWebRequest()); | |
| container.Register( | |
| AllTypes.FromAssemblyNamed("Foo.Domain") | |
| .Where(Component.IsInNamespace("Foo.Domain")) | |
| .LifestylePerWebRequest() | |
| .WithService | |
| .DefaultInterfaces()); | |
| } | |
| private static void BootstrapContainer() | |
| { | |
| var container = new WindsorContainer().Install(FromAssembly.This()); | |
| var controllerFactory = new WindsorControllerFactory(container.Kernel); | |
| ControllerBuilder.Current.SetControllerFactory(controllerFactory); | |
| ConfigureTypes(container); | |
| ServiceLocator.Container = container; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment