Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Pulling all instances for a base type out of StructureMap

*** There may be a better way to do this with StructureMap, but this is the way I was able to get this to work ***

Today I decided I wanted to move all my route mapping for Dimecasts out of the Global.asax file and into each of the various Controllers.  I wanted to do this for  two reasons.

  1. I was starting to build quite a list of custom route mappings
  2. I have decided that since routes map to a specific controller (in many cases, not all) the controller should be responsible for mapping its own routes

When I first decided that I wanted to perform this I tried to think of the various ways that I could accomplish allowing each controller register their own routes.  I was able to come up with 3 possible ways to achive this goal.

  1. Hard code the registration for each controller in my Global.asax file.
    This method is by far the simplest and would have gotten me to my end point the fastest, but it is simply too my friction in the long run.  I did not want to have to remember to add the register logic for each new controller that I added to the site. 

    So, in the end this is a non-option.

  2. Use reflection to find all the instances of my base controller and invoke the register logic.
    Using reflection seemed like an easy enough route.  In a few simple lines of code I could grab all the correct types and call invoke member.

    // Please note, this code has NOT been tested or used so it may not be 100% correct
    var controllers = Assembly.GetExecutingAssembly().GetTypes().
    		Where( x => typeof( BaseController ).IsAssignableFrom( x ) );
    foreach ( Type type in controllers )
    	type.InvokeMember("RegisterRoutes", BindingFlags.InvokeMethod, 
    			Type.DefaultBinder, type, new object[] { routes });

    However, in the long run this would not work either because I do not have any default, empty constructors on my controllers (hey, I am using IoC remember) so I would not be able to create an instance of each controller without massive amount of setup work.

  3. Use my IoC container (StructureMap) to get all instances and perform the register logic.
    Using my IoC container ended up being the direction I decided to go.  This method allowed me to get around the short coming of both options 1 and 2, and I could accomplish the task in a few lines of code.

    // Here I am going to pull out all registered plugins that are of type BaseController 
    // BaseController is my controller I base off Controllers off of
    var controllerInstances = ObjectFactory.Model.
    	PluginTypes.Where(x => typeof(BaseController).IsAssignableFrom(x.PluginType));
    foreach (var controllerInstance in controllerInstances)
    	var instance = (BaseController)ObjectFactory.GetInstance(controllerInstance.PluginType);

As you can see, using StructureMap to grab all instances based on a given type is pretty simple and pretty straight forward.  I was pretty happy because it took me longer to create this post then it did to get this logic up and running in the application.

Till next time,

[ --- Remember to check out ---]

Posted 12-05-2008 7:31 AM by Derik Whittaker


Comments wrote Pulling all instances for a base type out of StructureMap
on 12-05-2008 8:48 AM

You've been kicked (a good thing) - Trackback from

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)