Using Simple Injector within your Umbraco Controllers

  • Dan Lister

Holy crap! Has it really been three years since my last blog post? What better way to make a come back than talking about the awesome Umbraco and the equally awesome Simple Injector. Hopefully this post will help you implement your own dependency injection setup within your Umbraco application.

Install Simple Injector

To install Simple Injector within your Umbraco application, use NuGet to install Simple Injector's MVC package and it's package dependencies. You can either use Visual Studio's Package Manager Console or the NuGet executable itself.

Create a Base Controller Class

Now I'm not quite sure whether this step is needed but it is always nice to create a base class for your controllers. You could always add global things such as Output Caching. Having a base controller really helps with this.

Our BasePageController class will inherit from Umbraco's SurfaceController. It will also implement Umbraco's IRenderMvcController interface. The SurfaceController gives our controller access to Umbraco objects such as the CurrentPage. Implementing the IRenderMvcController interface allows our controller to be routed, since Umbraco handles all page routing for us. Within you base controller, we have 2 methods: One to load the current template and a second to find the template to use.

Create a Global Application Class

In order to setup dependency injection within our application, we'll need to create a new Global Application class where we can setup Simple Injector and register any interfaces and their implementations.

Within our global application class, we are doing two things: The first creating a new container which will hold all of our interfaces, implementations and controllers. We are also telling Umbraco and the .NET MVC application of our base controllers and dependency resolver. Lastly, we should tell our application to use our new global application class. We can do this by editing our global.asax file as follows.

Register Your Services

I'm assuming at this point you already have an interface and a class which implements a said interface. We have our Simple Injector container setup and registered with Umbraco and the application. Next we need to specify what implementations to use when interface dependencies are required within in our controller. To do this, we need to register each within our global application class, specifying the interface and implementation. It is also important to specify the lifestyle of the registration. This affects how and when implementations are created for each registration. For example, below we are using a scoped lifestyle and as the scoped lifestyle defined is a per web request scope, we will only find one instance of the registration per web request. Transient and Singleton or other examples of lifestyles.

Inject the Dependencies

All we have to do now is amend our controller to be dependant of a specific interface. As we have registered our implementations, the controllers will not care about what implementation it is given. The controller will be pretty happy to assume the given dependency conforms to the interface referenced. This makes testing our controllers quite easy. For example, we can mock our dependencies so that testing the controller becomes the sole purpose of our tests. So lets add some dependencies to our controller's constructor.

I think that explains everything or at least gives you the basics with regards to setting dependency injection, within Umbraco, using Simple Injector. If I've missed anything or you have spotted a mistake, please feel free to tweet me.