Resolving Dependencies in ASP.NET MVC 5 with StructureMap

In a previous post I showed how to use StructureMap with ASP.NET MVC 3.  It’s been a couple of years, so I figured it was time to update that article with the steps for getting StructureMap working in ASP.NET MVC 5.  If you’re interested in learning more about how to develop applications in a loosely coupled fashion, I highly recommend my course on SOLID Principles of Object Oriented Design to learn more (in particular, the Dependency Inversion Principle).

StructureMap is my preferred IOC container / dependency injection tool for .NET applications.  It’s free, performs very well, and has a number of very useful features that make it very productive to work with.  One example of a great use of StructureMap can be found in my CachedRepository article.

Assuming you’re starting with a new ASP.NET MVC 5 application, the easiest way to get StructureMap is using Nuget.  You can open the Package Manager Console and run this command:

install-package StructureMap.MVC5

which should result in something like this:

image

You can also locate and install the package using the Manage NuGet Projects option, available by right-clicking on the web project in Solution Explorer:

image

Once StructureMap is installed, you’ll notice several additions to your web project:

image

Most of these you can safely ignore.  The one file you’ll need to be concerned with is the IoC.cs class.  When you open it, you’ll see a single Initialize method where you will configure your interfaces and the types that should be used for them for this application.

image

To verify that things work, the simplest approach is to create an interface and an implementation of that interface, and wire them up in a controller.  If you’re using the default ASP.NET MVC 5 project template, the HomeController includes an About action that sets ViewBag.Message to a string, which is in turn displayed on the corresponding View.  We can demonstrate that everything works by replacing this string with the result of a method call via an interface.  Below is the revised code for the HomeController:

image

Run the application and visit the About page and you should see:

image

NOTE: We didn’t tell StructureMap how to resolve the IMessageProvider interface, or let it know anything about the MessageProvider implementation.  How did it figure this out?

In the default configuration in the IoC.cs file, there is a line of code that reads:

scan.WithDefaultConventions();

This code will automatically attempt to resolve any interface named IWhatever with an instance type named Whatever.  That is, the same name, but without the “I” prefix.  Of course you can still explicitly specify types that do not follow this convention, but you can save a lot of time if you want to write decoupled code but do not want to constantly have to update your container mapping information.

If you want to learn more about building loosely coupled applications using dependency injection, please check out my video training classes on N-Tier applications and Domain-Driven Design.

6 Comments

  • alex said

    <p>Great post as always, totally agree that StructureMap is the way to go.</p><p><a href="http://www.xml-editor.info" rel="nofollow">http://www.xml-editor.info</a></p>

  • paulb33 said

    <p>This is a grate post. However the StructureMap.MVC5 package has changed a bit since this post and it could be valuable to update your screenshots etc.</p>

  • mhesabi89 said

    <p>I was using x.For&lt;iunitofwork&gt;().HttpContextScoped().Use&lt;dbcontext&gt;(); in previous version but seems it doesn't supported in current version. <br>What should I use instead of HttpContextScoped() ?</p>

  • Thiago Silva said

    <p>May be worth pointing out that if you use Web API in your MVC project, or just have a standalone (i.e. not a full MVC web app), you should be using the StructureMap.WebApi2 nuget package. That package has a dependency on the MVC5 package but adds the required code for dependency resolution from ApiControllers.</p>

Add a Comment