Configuring the Castle Windsor container

Just recently I stumbled across a code sample, which showed how to use the Registry-class of StructureMap to register classes to the container. Basically this looks like this:

public class RepositoryRegistry : Registry
{
    public RepositoryRegistry ()
    {
        For(typeof (IRepository<>)).Use(typeof (DummyRepository<>));
    }
}
container = new StructureMap.Container(x =>
{
    x.AddRegistry(new RepositoryRegistry());
});

So I though, this must also be possible in Castle Windsor – and indeed! So the equivalent in Castle Windsor would look like this:

using System;
using System.Reflection;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
public class RepositoryInstaller:IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes
                               .Pick()
                               .FromAssembly(Assembly.GetExecutingAssembly())
                               .If(s => s.Name.EndsWith("Repository"))
                               .Configure(c => c.LifeStyle.Singleton));
    }
}
container = new WindsorContainer().Install(
    new NHibernateInstaller(sessionSource),
    new RepositoryInstaller(),
    new PresenterInstaller()
);

This is really cool, too bad I didn’t discover that before 🙂

2 Comments

Leave a Comment.