My God... could I put any more acronyms in that title. Yeah, I probably could. Oh, and if you just came for the code, jump to the bottom.
In his post TDD and Dependency Injection with ASP.NET MVC, Phil used a DI framework to reduce friction and increase his Test-Fu when doing the TDD thang.
How do you up your Test-Fu? By pushing the monotonous, and sometimes overbearing, work of wiring up dependencies off to a tool - in this case, a Dependency Injection tool called StructureMap.
Angle bracket madness
Phil was good enough to show us how to configure StructureMap via it’s default mechanism, the StructureMap.config file. And while I’m all for outsourcing the boring work so I can work less yet do more, one thing I really hate is angle-brackets. And the one thing I hate even more than angle bracket programming is programming with magic strings.
<?xml version="1.0" encoding="utf-8" ?>
<StructureMap>
<Assembly Name="MvcApplication" />
<Assembly Name="System.Web.Extensions
, Version=3.6.0.0
, Culture=neutral
, PublicKeyToken=31bf3856ad364e35" />
<PluginFamily Type="System.Web.Mvc.IController"
Assembly="System.Web.Extensions
, Version=3.6.0.0
, Culture=neutral
, PublicKeyToken=31bf3856ad364e35"
DefaultKey="HomeController">
<Plugin Type="MvcApplication.Controllers.HomeController"
ConcreteKey="HomeController"
Assembly="MvcApplication" />
<Plugin Type="MvcApplication.Controllers.BlogController"
ConcreteKey="BlogController"
Assembly="MvcApplication" />
</PluginFamily>
<PluginFamily Type="MvcApplication.Models.IPostRepository"
Assembly="MvcApplication"
DefaultKey="InMemory">
<Plugin Assembly="MvcApplication"
Type="MvcApplication.Models.InMemoryPostRepository"
ConcreteKey="InMemory" />
</PluginFamily>
</StructureMap>
The bold lines are changes I had to make to the original configuration file to get the demo application to actually run.
And what do we see all over the config file? Angle brackets and magic strings. Lucky for us, Jeremy added a fluent interface to StructureMap 2.0 that lets us leave all of those ugly angel brackets behind... and we can even get rid of some of those magic strings!
Using the fluent interface
To keep it simple, and because I’m lazy, I decided to grab Phil’s demo source code and modify it to use the fluent API rather than the default XML configuration. Savvy?
With code in hand I fired up Visual Studio and headed straight for the Application_Start method. I’m really just using that method to call my own ConfigureContainer method, which actually does the work of configuring StructureMap. For consistency sake I want my fluent configuration code to do the same thing that Phil’s XML based configuration did.
It ended up looking like this (I used a screen shot too keep it more readable):
As you can see there are no only a few angle brackets, use for Generics, but that’s a heck of a lot less than the XML had. I was also able to get rid of a few of the magic strings by using
.UsingConcreteType<MyClassHere>().WithName(typeof(MyClassHere).Name)
And with that we now have the IoC container configured and working exactly like it was using the StructureMap.config file. Don’t believe me? Grab the code (below) and take a look for yourself. I even added an AppSettings key to the web.config that allows you to switch back and forth between how StructureMap gets configured.
Resources
Want to take a spin thru the code? Grab it here: [DOWNLOAD]
Also, don’t forget to check out Phil’s original post for way more information about using TDD + DI to make mvc.net easier for U.