ASP.NET MVC: Let StructureMap create your controllers
For those of you already familiar with StructureMap and want to use it to configure your objects in ASP.NET MVC, read on.
The ASP.NET MVC framework has a default controller factory (DefaultControllerFactory) that requires all controllers to have a parameterless constructor. So, if you want to inject your dependencies to the constructor, you can’t do it. In addition, Microsoft suggests the following pattern to mock out the dependencies when testing:
The Microsoft way of testing
(Example from www.asp.net/mvc)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //The Microsoft way... public class GroupController : Controller { private IContactManagerService _service; public GroupController() { _service = new ContactManagerService(new ModelStateWrapper(this.ModelState)); } public GroupController(IContactManagerService service) { _service = service; } //... } |
In other words, create one constructor for the actual application, and one constructor for testing. It works, you get to test it, it’s not all that bad, but it just doesn’t sit well with me.
Take control of your controllers!
Don’t you like this either? Well, there is an easy way out of it. One of the really good things about ASP.NET MVC is that it is easy to configure. You can create your own controller factory and take control of the whole process. It might sound like a complex task, but it isn’t.
Let’s start with our controller, it has a dependency that is injected through the constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class HomeController : Controller { private readonly Speaker speaker; public HomeController(Speaker speaker) { this.speaker = speaker; } public ActionResult Index() { ViewData.Model = speaker; return View(); } } |
Running the website now, trying to reach this controller, will make the framework crash (”No parameterless constructor defined for this object.”). We need our own controller factory to make this work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class StructureMapControllerFactory : DefaultControllerFactory { private readonly IContainer container; public StructureMapControllerFactory(IContainer container) { this.container = container; } protected override IController GetControllerInstance(Type controllerType) { return (IController)container.GetInstance(controllerType); } } |
This controller takes in a StructureMap.IContainer and uses it to create all the controllers. It inherits from the DefaultControllerFactory and overrides the GetControllerInstance-method that is used to create controllers.
Now all we have to do is to tell the ASP.NET MVC framework that it should use our controller factory. To do that simply add this line in the Application_Start() method in the Global.asax file:
1 | ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(new Container())); |
(Notice that we do not need to configure StructureMap since our dependencies are so easy to resolve. In a real world app you will most probably need to configure the container)
There you go. Using StructureMap with the ASP.NET MVC framework is easy!
Download the complete example here
-Tore Vestues


It amazes me that Phil didn’t add these to the framework by default as a option for you to configure. I asked him about it at NDC and there are no plans in adding these either. The ones provided by the MVC Contrib project (http://mvccontrib.codeplex.com/Wiki/View.aspx?title=Documentation) are what they want us to use. It is a shame that even do the framework is much better then WebForms it is still not there yet.
An other good thing you should (perhaps already did) look into is the Fluent Html Helpers from the MVC Contrib or the Opinionated Input Helpers (http://www.lostechies.com/blogs/hex/archive/2009/06/09/opinionated-input-builders-for-asp-net-mvc-using-partials-part-i.aspx) from Eric Hexter which perhaps I like even more.
June 28th, 2009 at 14:33 (648)[...] In my last project we used the ASP.NET MVC framework along with StructureMap, which made us create our own controller factory. I just blogged about it here. [...]
July 3rd, 2009 at 11:05 (503)In the jungle of solutions that don’t work and that include to much it’s always amassing how simple things can be done.
March 1st, 2010 at 10:03 (460)Thanks Tore
testing to see how well this works
March 12th, 2010 at 4:03 (210)Its like you read my mind! You appear to understand a lot about this, like you wrote the guide in it or something. I think that you can do with some p.c. to force the message house a bit, however other than that, this is excellent blog. A great read. I will definitely be back.
October 18th, 2011 at 23:58 (040)