How To Set-Up Structure Map With Umbraco 7

This post will be the first in a series that will cover how to unit test your Umbraco website.  The first step to unit testing any project is implementing an Inversion Of Control container within your project.  There are a number of IOC frameworks available; from my experience StructureMap is pretty easy to use and implement so it’s as good as any.  In today’s post, I will be covering how to set-up structure map.

Installing The StructureMap Package

The first thing we need to do is install structure map into your web project.  Luckily, there’s an MVC package that removes all the hassle of setting structure map up for us, in Visual Studio go to the Nugent Package Manager and find the ‘StructureMap.MVC5’ (assuming you’re running MVC5). Change the version of MVC to the one you want to run.

structuremap_mvc5_nuget

Creating Some Dependencies

The next step is to create some very basic dependencies so we can test we have everything set-up correctly.  To do that let’s create two classes, an interface and a concrete implementation of that interface.

public interface IUmbracoDependencies
{
}

and

public class UmbracoDependencies : IUmbracoDependencies
{
public UmbracoDependencies()
{
}
}

Configuring Structure Map

The next step is to configure structure map to use our new interfaces.  When you installed the Nuget package, a number of classes will have been installed in your project.  The main one you care about, as a developer, is the file called ‘IOC.cs’ found in a folder called ‘Dependencies’.  When you first look in Ioc.cs you should see something like this:

public static class IoC {
public static IContainer Initialize() {
return new Container(c => c.AddRegistry<DefaultRegistry>());
}
}

What we need to do is register our interface with structure map and configure it to pass in our concrete class ‘UmbracoDependencies’. To do this we do:

public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IUmbracoDependencies>().Use<UmbracoDependencies>();
});
return ObjectFactory.Container;
}
}

The line that does all the magic is:

x.For<IUmbracoDependencies>().Use<UmbracoDependencies>();

This is the bit that configures structure map. When you want to add in your own dependencies, just rinse and repeat. The part above is also quite important:

x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});

What this is telling structure map, that on application start, scan all the classes in the same assembly and for any interface that is called ‘ISomething’, try to find a default concrete implementation called ‘Something’.

Lastly, the last time I tried implementing this code I got an error about ‘TheCallingAssembly’. If you get the same issue you simply need to add this using statement into the IOC.cs

using StructureMap.Graph;

Testing It Works

I’m assuming you have a homepage controller configured and running:

public class HomePageController : Umbraco.Web.Mvc.RenderMvcController
{
private readonly IUmbracoDependencies dependency;
public HomePageController(IUmbracoDependencies dependency)
{
this.dependency = dependency;
}
public ActionResult homepage(RenderModel model)
{
var homepage = ContentHelper.GetByNodeId<HomepageDocumentType>(model.Content.Id);
var homepageViewModel = new HomepageViewModel(homepage);
return CurrentTemplate(homepageViewModel);
}
}

When you run your web project the constructor will be called and the concrete class called ” will be passed in.  If you haven’t set structure map up correctly you will see a ‘No parameterless constructor defined for this object’ error message.  This means that structure map can’t inject the dependency for whatever reason.  Most of the time it’s either bad configuration in the Ioc.cs

Conclusion

In today’s guide, we’ve gone through all the steps in order to enable dependency injection within our Umbraco website. This tutorial on its own isn’t very useful, but the steps in here are the building blocks to allow us to do more useful things like unit test our code later on.

To get started with structure map in Umbraco, install the StructureMap.MVC5 package.  This does all the hard work for you so all you need to do is configure the IOC.cs file.  In here we simply create maps from our interfaces to our concrete types.

I’ve only gone over a very basic example.  What happens when you want to inject a concrete class which takes in parameters in the constructor.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *