Learning about StructureMap

I’ve been playing around with StructureMap for the last couple of days and I must say that I’m really impressed. I had no troubles in quickly getting up to speed with this amazing IoC container. Jeremy claims that StructureMap is actually one of the first IoC containers in the .NET space. Although it has been around for a long time, somehow it has always been under (or above) my radar until my colleague Peter pointed out that I should take a look. I’ve always been a Castle Windsor fan boy, but using StructureMap for the past couple of days challenged my assumptions about IoC containers.

Anyway, I’m not going to repeat all of the good stuff that has already been written about StructureMap. Reading this post (also take a look at the linked articles) and watching these Dime Casts should get you started fairly quickly.

With this post I’m going to show you some of my personal favorite features.

1/ No explicit configuration required.

Suppose I have a simple message handler class with two dependencies. The usual Castle Windsor configuration looks like this:

var messageHandler =  


The StructureMap configuration for the same scenario looks like this:


ObjectFactory.Initialize(registry =>
var messageHandler = 


Notice that StructureMap doesn’t require you to register the CreateUserMessageHandler class! It’s a small detail, but a nice one.

2/ Diagnostics

If you want to provide a unit test that checks whether the configuration of the container is valid, you can use the following method :



If something is wrong, a StructureMapConfigurationException is thrown. It doesn’t get any easier than this.

3/ Profiles and Contextual Binding

Using profiles in StructureMap, you can basically switch container configuration based on a context. Here is a code sample that illustrates this concept:


ObjectFactory.Initialize(registry =>
ObjectFactory.Profile = "ActiveDirectory";
var ldapStore = ObjectFactory.GetInstance<ILdapStore>();
Assert.That(ldapStore, Is.TypeOf(typeof(ActiveDirectory)));
ObjectFactory.Profile = "Fedora";
ldapStore = ObjectFactory.GetInstance<ILdapStore>();
Assert.That(ldapStore, Is.TypeOf(typeof(Fedora)));

With a single line of code you can switch configuration. Again, quite easy.

4/ Custom Instance Creation

Castle Windsor provides a FactorySupport facility which enables you create your own instances for a certain type. StructureMap provides the same option through its fluent interface:


ObjectFactory.Initialize(registry =>
    .Is.ConstructedBy(() => new OpenLdap("DC=Jan,DC=BE"));
var ldapStore = ObjectFactory.GetInstance<ILdapStore>();

The fluent interface of StructureMap makes this very readable.

5/ Auto Mocking

Ayende provided an implementation of an auto mocking container in his Rhino Tools repository that uses Castle Windsor. StructureMap provides a similar API straight out of the box. No separate downloads required. You can read this article for more information. I know that there are numerous people out there who resist the idea of an auto mocking container, but it simply makes setting up mock objects and the subject-under-test less tedious.


As I mentioned before, I’ve been a Castle Windsor fanatic for quite some time now. The sweetness of StructureMap puts me into a mind struggle about which IoC container I like the most. To me, one of the most compelling features of Castle Windsor is its extensibility with custom facilities. I didn’t notice any equivalent feature in StructureMap. I could be wrong however, so please let me know if it does have a form of add-on capabilities. On the other hand, the ease of use and well thought out API of StructureMap makes it serious candidate as well. Anyone on my current team could start using it in no time. That’s a very important aspect as well.

Anyway, if you are looking for an IoC container, make sure to take StructureMap into account as well. Its the small subtleties in the API that makes it so nice to use.

Published by

Jan Van Ryswyck

Hi, thank you for visiting my blog and reading all the crap that I'm posting here. I'm a senior software engineer at SD WORX. Developing software is one of my greatest passions in life, and I enjoy doing it every single day. I've got three kids (Len, Lisa & Laura) who constantly remind me that there is more in life than just programming all day. They are the greatest kids in the whole world. And last but not least, there's my girlfriend who is my inspiration in life. You can always contact me at jan_dot_van_dot_ryswyck at gmail.com

3 thoughts on “Learning about StructureMap”

Comments are closed.