March 02, 2011 03:01 PM

Using IoC with Structure Map 2.6 and MVC 3.0

Michael K. Campbell's tips for using the StructureMap Inversion of Control (IoC) container with ASP.NET MVC 3
Dev Pro
InstantDoc ID #129819

Inversion of Control (IoC) provides developers with a great way to componentize and modularize their code, while also serving as a fantastic resource for encouraging and enabling true unit testing. To that end, I've been using StructureMap as an IoC container. Only, I recently found that wiring it up to take advantage of ASP.NET MVC 3.0's new features that support IoC was a bit more difficult than anticipated. So, I decided to share what I've learned up to this point.

MVC 3.0 and IoC
A few months ago I contemplated what would happen if I needed or wanted to change out IoC Containers—say, to switch to Ninject or Unity instead of using StructureMap. The part of my brain (that likes to overbuild things) instantly realized that I should probably start building my applications with some sort of abstraction around IoC Containers and injection—so that I could more easily swap out different containers if or as needed. However, the more practical part of my brain (that likes to finish things) happily realized that while building such an abstraction would be fun, it wouldn't provide any immediate benefit. So, in a spirit of YAGNI and a realization that I could "burn that bridge if and when I came to it," I decided to move on to other things.

Fast forward a few months to when ASP.NET MVC 3.0 was released, and I was glad I hadn't wasted any time on abstracting IoC functionality—as the ASP.NET MVC team had done a brilliant job of it.

Specifically, MVC 3.0 provides a new DependencyResolver "intrinsic" that you can load up with your IoC container details and objects as needed; then you can just query DependencyResolver directly when you need to look up or locate services or objects instead of directly interacting with your IoC container of choice. The result, of course, is that (once again) a set of abstractions helps make code that much easier to maintain and extend when or if needed.

Getting MVC 3.0 And StructureMap 2.6 To Play Nicely Together
Only, getting all this sorted out in MVC 3.0 is more painful than I'd like to admit. A big part of the pain involved in getting IoC working with MVC 3.0 is that there just isn't much in the way of documentation yet. Yes, Brad Wilson has done a great job of providing an 11-part series in his blog on Service Location, but most of that content was written before MVC 3.0 was officially released or RTM'd.

Nor does it help that the documentation for StructureMap 2.6 is quite sparse, especially since StructureMap 2.6 represents some big changes over its predecessor. Or, more specifically: There are some big syntactical and functional changes that have made their way into StructureMap 2.6—which was officially released only very recently. Consequently, for someone like me who was only just starting to get the hang of effectively working with Service Location and Inversion of Control, getting StructureMap 2.6 and MVC 3.0 to play nicely together took more effort than I would have liked.

Happily though, I didn't have to figure everything out on my own, as I was able to pinch and crib a number of different features or facets of how to get everything working from Javier Lozano, Steven Smith, and K. Scott Allen (along with copious amounts of StackOverflow thrown into the mix).

An End-to-End Sample App
The point though, was that I had to cobble lots of different things together. So, given how much difficulty I found in locating an end-to-end sample application that showed how to use MVC 3.0 and StructureMap together, I decided to provide an end-to-end sample of my own. (Email me at mike@overachiever.net if you'd like a copy of the sample application—which includes some additional notes and info that didn't make it into this article.)



ARTICLE TOOLS


Comments
  • Michael K. Campbell
    1 year ago
    Mar 03, 2011

    As an FYI, I finally figured out what I was missing in my sample app to get Setter Injection to work with Attributes.

    Specifically, I was missing the step where I told my MVC application to use my custom 'SiteFilterProvider' as the FilterAttributeFilterProvider for the application - which, in turn, was set up with IoC capabilities.

    Brad Wilson's blog post outlines the critical step where he modifies the Global.asax's Application_Start() method here:
    http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html

    I also had to bolster my custom SiteFilterProvider to provide two new overrides so that all of my different KINDS of filters/attributes we're being correctly 'built up' by Structure Map.

    The Sample Application that I have provided is therefore fully functional - and contains all of these fixes/changes.

    --Michael K. Campbell

You must log on before posting a comment.

Are you a new visitor? Register Here