Firstly some conceptual details. In an ASP.NET MVC application the typical entry point for a page request is a controller. We want the Inversion of Control container to resolve our controllers for us, because then any dependencies that the controllers have can also be automatically resolved simply by listing the dependencies as arguments in the controllers' constructors.

Confused yet? Here's an example of how you'd use IoC, after it is all set up. I think explaining it this way makes things easier!

public class HomeController : Controller { // lets say your home page controller depends upon two providers private readonly IMembershipProvider membershipProvider; private readonly IBlogProvider blogProvider; // constructor, with the dependencies being passed in as arguments public HomeController( IMembershipProvider membershipProvider, IBlogProvider blogProvider) { this.membershipProvider = membershipProvider; this.blogProvider = blogProvider; } // so taking your Registration example... [HttpPost] public ActionResult Register( Web.Models.Authentication.Registration model) { if (ModelState.IsValid) { this.membershipProvider.CreateUser(model.Email, model.Password); } // If we got this far, something failed, redisplay form return View(model); } }

Note that you have not had to do any resolving yourself, you have just specified in the controller what the dependencies are. Nor have you actually given any indication of how the dependencies are implemented - it's all decoupled. It's very simple there is nothing complicated here :-)

Hopefully at this point you are asking, "but how does the constructor get instantiated?" This is where we start to set up your Castle container, and we do this entirely in the MVC Web project (not Persistence or Domain). Edit the Global.asax file, setting Castle Windsor to act as the controller factory:

protected void Application_Start() { //... ControllerBuilder.Current .SetControllerFactory(typeof(WindsorControllerFactory)); }

...and define the WindsorControllerFactory so that your controllers are instantiated by Windsor:

/// Use Castle Windsor to create controllers and provide DI public class WindsorControllerFactory : DefaultControllerFactory { private readonly IWindsorContainer container; public WindsorControllerFactory() { container = ContainerFactory.Current(); } protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType) { return (IController)container.Resolve(controllerType); } }

The ContainerFactory.Current() method is static singleton that returns a configured Castle Windsor container. The configuration of the container instructs Windsor on how to resolve your application's dependencies. So for example, you might have a container configured to resolve the NHibernate SessionFactory, and your IMembershipProvider.

I like to configure my Castle container using several "installers". Each installer is responsible for a different type of dependency, so I'd have a Controller installer, an NHibernate installer, a Provider installer for example.

Firstly we have the ContainerFactory:

public class ContainerFactory { private static IWindsorContainer container; private static readonly object SyncObject = new object(); public static IWindsorContainer Current() { if (container == null) { lock (SyncObject) { if (container == null) { container = new WindsorContainer(); container.Install(new ControllerInstaller()); container.Install(new NHibernateInstaller()); container.Install(new ProviderInstaller()); } } } return container; } }

...and then we need each of the installers. The ControllerInstaller first:

public class ControllerInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( AllTypes .FromAssembly(Assembly.GetExecutingAssembly()) .BasedOn<IController>() .Configure(c => c.Named( c.Implementation.Name.ToLowerInvariant()).LifeStyle.PerWebRequest)); } }

... and here is my NHibernateInstaller although it is different to yours, you can use your own configuration. Note that I'm reusing the same ISessionFactory instance every time one is resolved:

public class NHibernateInstaller : IWindsorInstaller { private static ISessionFactory factory; private static readonly object SyncObject = new object(); public void Install(IWindsorContainer container, IConfigurationStore store) { var windsorContainer = container.Register( Component.For<ISessionFactory>() .UsingFactoryMethod(SessionFactoryFactory)); } private static ISessionFactory SessionFactoryFactory() { if (factory == null) { lock (SyncObject) { if (factory == null) { var cfg = new Configuration(); factory = cfg.Configure().BuildSessionFactory(); } } } return factory; } }

And finally you'll want to define your ProvidersInstaller :

public class ProvidersInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { var windsorContainer = container .Register( Component .For<IMembershipProvider>() .ImplementedBy<SubjectQueries>()) .Register( Component .For<IBlogProvider>() .ImplementedBy<SubjectQueries>()); // ... and any more that your need to register } }

This should be enough code to get going! Hopefully you're still with me as the beauty of the Castle container becomes apparent very shortly.

When you define your implementation of your IMembershipProvider in your persistence layer, remember that it has a dependency on the NHibernate ISessionFactory . All you need to do is this:

public class NHMembershipProvider : IMembershipProvider { private readonly ISessionFactory sessionFactory; public NHMembershipProvider(ISessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }

Note that because Castle Windsor is creating your controllers and the providers passed to your controller constructor, the provider is automatically being passed the ISessionFactory implementation configured in your Windsor container!

You never have to worry about instantiating any dependencies again. Your container does it all automatically for you.