In this tutorial, you will learn how to install and use SimpleInjector with Episerver CMS. From version 7 onwards, Episerver CMS has shipped with StructureMap. This means dependency injection and inversion of control come out-of-the-box with Episerver. If you are new to Episerver CMS, on most projects I recommend you stick with StructureMap unless you have a good reason not to. Just because Episerver ships with StructureMap, it doesn't necessarily mean that you need to use it to manage your custom dependencies. If you are struggling to get your head around StructureMap, an easier solution to help you get started with DI in your project could be SimpleInjector . So if you hate StructureMap, or you can't figure out how to configure it, this is the tutorial for you πŸ”₯πŸ”₯πŸ”₯

Why Simple Injector?

StructureMap was released in 2004 and since then there are now loads of new DI containers available for .NET. Simple Injector as the name implies, has been the simplest and easiest IOC framework that I have used. It took me less than 10 minutes from first installing it, to get it up and running correctly. So if you are struggling with StructureMap, this is your simplest alternative approach πŸ€”

TIP: For those of you who just want to learn StructureMap, I recommend reading this and this.

Installing Simple Injector

You can install Simple Injector via NuGet. In Visual Studio open NuGet Explorer:

Tools ➑ NuGet Package Manager ➑ Manage NuGet Packages for Solution

This will open NuGet exporer :

A Very Simple Dependency Injection Technique For Episerver 1

In the Online option, look-up 'SimpleInjector' and you should see the above packages. If you want Simpler Injector to work with Web API or MVC you need to install extra integration packages WebApi.SimpleInjector and MVC.SimpleInjector

How To Configure SimpleInjector for MVC: Configuration is where the simplicity of SimpleInjector really shines. Open your global.ascx and find your Application_Start method (create one if it doesn't exist). In here, add the following code:

Configuring Simple Injector for Web API: Configuring SimpleInjector to work with Web API is also simple:

Note that on Line 5, I register a dependency called IType. We will use this in an example below 😊 The set-up of most IOC containers is fairly complex. As you can see it only takes a few lines of code to configure SimpleInejctor. It is so easy and minimal, anyone new to the area of dependency injection can try it out without the steep learning curve. Your configuration code will be located all in one place without having to add loads of other classes 😊

How To Set-up A Dependency Using SimpleInjector

Say we have this interface that you want to use with dependency injection:

With a concrete implementation class that looks like this:

To use this code with dependency injection, you would inject the dependency as a constructor parameter within a controller. An example of how to do this is shown below:

As long as you have the correct SimpleInjector Nuget packages installed, you have the correct configuration within the Application_Start method in your global.ascx and you register your dependency correctly (see the web API code sample above), SimpleInjector will automatically create a new instance of myType and inject it into the constructor of the controllerπŸ’₯

You do not need to do any other config. That's all it takes to get started. You are free to get on with your work. Just because Episerver ships with Structure map doesn't necessarily mean that you have to use it. As I hope I've demonstrated, Simple Injector is a very simple alternative to providing dependency injection into your project. if you are struggling with StrucutreMap, this might be an alternative to consider. Happy Coding 🀘