Skip to content

Dependency Injection with Castle Windsor

simonthorogood edited this page Dec 12, 2010 · 1 revision

To use Castle Windsor as your dependency resolver in OpenRasta, do the following:

  • Add a reference to the OpenRasta.DI.Windsor assembly
  • Add a reference to the Castle Windsor assemblies (Castle.Core, Castle.DynamicProxy, Castle.Windsor, Castle.MicroKernel)
  • Implement IDependencyResolverAccessor. One handy place to do this is in your main Configuration class.
  • In your IDependencyResolverAccessor, return a new WindsorDependencyResolver(IWindsorContainer). Use your existing container initialisation strategy, a simple example of which follows:

In a standard ASP.NET application, you might choose to implement the IDependencyResolverAccessor in global.asax, which would look like this:

  public class Global : HttpApplication, IDependencyResolverAccessor
  {
      IWindsorContainer container;
      public IWindsorContainer WindsorContainer
      {
          get
          {
              if(container == null)
                  container = ConfigureContainer();
              return container;
          }           
      }

      IWindsorContainer ConfigureContainer()
      {
          container = new WindsorContainer();

          container.Register(
              Component.For<IUserRepository>()
                  .ImplementedBy<UserRepository>());

          return container;
      }

      public IDependencyResolver Resolver
      {
          get
          {
              return new WindsorDependencyResolver(WindsorContainer);
          }
      } 
  }
Clone this wiki locally