A Very Simple Dependency Injection Technique For Episerver

Dependency injection and inversion of control are not new concepts in Episerver. From version 7 onwards, injecting the Episerver API has become a standard, which is great. I've talked about Structure map and Episerver many times before, if you want to know a bit more then I recommend reading, Episerver Structure Map Guide and Dependency Injection In Episerver – ServiceLocator and Injected Explained. Just because Episerver ships with Structure map, it doesn't necessarily mean that you need to use it. There are loads of DI containers available from Nuget, I've been using IOC containers for about 6 years and in that time, I've used StructureMap, Ninject, Castle Windsor, which all have their strengths and weaknesses. Recently, I tried working with Simple Injector and as the name implies, it is the easiest IOC to get started within a project. It took me less than 10 minutes from first installing it, to get it up and running correctly. In today's guide, I'm going to cover this process so you can start using dependency injection within your projects.

Installing Simple Injector

First off, you will need visual studio open and the Nuget package manager. In Visual Studio open up Nuget, 'Tools' -> 'NuGet Package Manager' -> 'Manage NuGet Packages for Solution' sitecore_simple_injector In the Online option, look-up 'SimpleInjector' and you should see the above packages. In my example, I also wanted to use Simpler Injector with Web API and MVC, so, I installed the extra integration packages 'WebApi' and MVC'.

Configuring Simple Injector for MVC

This is where the simplicity of simple injector really shines. Open your global.ascx and find 'Application_Start'. In here, all you need to do is define the following code for MVC:
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

// Register your types
container.Register<IType, MyType>();

// Register The Container
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.RegisterMvcIntegratedFilterProvider();

// This is optional
container.Verify();  

DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

Configuring Simple Injector for Web Api

Web API is also very similar
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

// Register your types
container.Register<IType, MyType>();

// Register The Container
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
Compared to a lot of IOC containers that takes a lot code to set-up and register the container, this is so easy and minimal, anyone new to the area of dependency injection can try it out without the steep learning curve of some of the other DI containers. Your code is all in one place without having to add loads of configuration, you can just get on with the task at hand.

A Simple Example

Say we have this code:
public interface IType
{
  public string Hello { get; set; }
}
With this class
public class MyType : IType
{
  public string Hello { get; set; }
}
When we run this code:
public class EnquiryController : ApiController
{
   public EnquiryController(IType myType)
   {
      this.myType= new myType();
   }
}
If you tried to run this code, as long as you have the Simple Injector Nuget packages installed and have the container defined in your Global.ascx's Application_Start, the myType will automatically be populated and you're free to get on with your work without having to worry about any type of dependencies within your class.

Conclusion

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.

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

Back to top