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

More Posts

2 replies
  1. Vincent
    Vincent says:

    Episerver doesn’t seem to give us a hook to replace IOC container completely, that’s one of the main reason i stick with structuremap, so it won’t confuse other devs who later need to support it. Second reason is personal preference. I really like the design of ServiceConfiguration attribute , it allows me to register the concrete class anywhere at anytime without going back to the Ioc bootstrap class.

    Reply
    • Jon D Jones
      Jon D Jones says:

      Yes, EpiServer and Structure map are quite tightly coupled and you would still need to have the Structure map package installed in your project, theres no escaping that 😛 I usually tend to go with separation of concerns, so I to try and keep configuration and code separated in it’s own class so I’ve never really used the ServiceConfiguration attribute a lot. I can see why people use ServiceConfig attribute and I think it does make life a lot easier, it’s just not my architectural style

      I’ve worked on projects where we needed a lot of structure map configuration to get dependencies injecting correctly, with the right constructor parameters etc.. the structure map classes then become quite large and complex, Simple Injector allows you do do a lot with so much less set-up code. It’s not right for every project but people new to DI might benefit from its simplicity. If you’re making a simple brochureware site as well, then less is best sometimes. If I were working on a commerce site, or, a large build I’d always stick with structure map, but that’s probably more because I’ve been using it for 5 years

      Reply

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 *