Register concrete types against the first interface

Most of my IoC experience has been using StructureMap, with the default conventions:

container.Configure(c => c.Scan(s =>
    {
        s.Assembly(GetType().Assembly);
        s.LookForRegistries();
        s.WithDefaultConventions();
    }));

Which registers any components whose name matches the interface, minus the I, e.g. ThingyController : IThingyController, but not DefaultThingyRepository : IThingyRepository.

Recently however, I’ve worked on a few projects using Castle Windsor, whose auto-registration uses the first interface:

 container.Register(AllTypes
    .FromAssembly(GetType().Assembly)
    .Pick()
    .WithService.FirstInterface()
    .Configure(req => req.LifeStyle.Transient));

What a great idea, I thought! Why can’t I do that with StructureMap? Well the short answer is, you can:

container.Configure(c => c.Scan(s =>
    {
        s.Assembly(GetType().Assembly);
        s.LookForRegistries();
        s.RegisterConcreteTypesAgainstTheFirstInterface();
    }));

Hurray!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s