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 … more
Contents tagged with structuremap
I found myself explaining inversion of control containers and their benefits to someone today, and so I created a very simple console application that makes use of StructureMap (my favorite such container). You can view the whole file in this Gist. Here I’ll just briefly explain what’s going on. The main benefit of a tool like StructureMap is to decouple your classes from their collaborators. Rather than classes knowing exactly which implementations they’re working with, they publish their needs for collaborators in their constructors, providing explicit dependencies (see my other posts on dependencies). The IoC container then is used to provide these classes with the … more
At the moment (18 July 2012) if you install the NuGet package structuremap.MVC4, it will not compile due to breaking changes in the ASP.NET MVC4 RC distribution. You have to jump through some extra hoops to get this working. Here are two posts that help: Configuring MVC4 with StructureMap Using the Web API Dependency Resolver However, even with both of these getting things working wasn’t quite as easy as I would have liked. Here are two full files with namespaces showing what I used to get things working (Gist 3135943). I assume the NuGet package will be updated to be correct once MVC4 ships. more
Update: If you want to use StructureMap with ASP.NET MVC 5, I have a walkthrough on this now as well.
As I write this, the best resource for official documentation on ASP.NET MVC 3 is of course MSDN. You can also learn more about ASP.NET MVC 3 here. However, neither of those mention how to properly set up an IOC Container (like StructureMap) with ASP.NET MVC 3. After some searching, I was able to get things working using the RTM version of ASP.NET MVC 3. Here’s what I had to do.
Step One – Global.asax – Application_Start()
First, you need to wire things up when your application starts. You can put all of your container registration stuff … more