In this tutorial, you will learn the basics of performing dependency injection within Episerver CMS. Dependency injection is a fairly standard computer science topic nowadays, however, I feel a lot of developers still struggle with how dependency injection works within Episerver. The good news is that Episerver ships with dependency injection out-of-the-box. If you want to learn how to use dependency injection within your Episerver project, read on 🔥🔥🔥

The ServiceLocator

The first thing you need to understand about dependency injection within Episerver is the ServiceLocator. ServiceLocator is the most frequently used technique by developers to get access to dependencies within Episerver. This includes Episervers own APIs!

In the olden days of Episerver 6, you would have used the DataFactory API (which implemented a singleton pattern) to query Episerver for content in code. The data factory pattern created a lot of code smells, especially if you wanted to unit test your code. As DataFactory couldn't be run within a unit test, it was close to impossible to unit test Episerver projects. In Episerver 7 onwards, we're provided with the ServiceLocator for accessing the APIs that will allow you to do the same sort of things as DataFactory. The service locator allows us to work with interfaces rather than implementations. This is a good start to making our website more unit testable. To code to access the IContentRepository API in order to query Episerver for content, now looks like this:

Using interfaces and an injection pattern to access the Episerver API means we can now substitute the Episerver implementations with mocks. Mocking will give you a lot more power and control when writing unit tests. Using the above code we could now run the below test and swap out the API call that talks to the Episerver database within a mocked version instead:

ServiceLocation is a big improvement for API access compared to the old days, however, it has some limitations. What are they 🤔


Episerver also provides an alternative way of injecting dependencies into your code, via Injected<T>. Injected<T> uses property injection via the IoC container to give you access to your dependencies within your code. An example of how to use Injected<T> can be seen below:

Injected<T> is really useful when you need to implement things like ISelectionFactory or an InitializationModule. Certain special base classes will not allow you to define your own custom constructors as they define their own private constructors. In these instances, you can use Injected<T> that uses property injection instead!

Registering Your Own Code To Work With ServiceLocator

The last Episerver dependency injector tool we will cover today is the [ServiceConfiguration] attribute. If you decorate the [ServiceConfiguration] attribute on top of a custom class you create, you can use either ServiceLocator or Injected<T> to access that class from anywhere in your codebase. The code to do this would look like this:

After you register your custom interface you can use this code to access to it:

That covers the main Episerver dependency injection tools that you have within your toolbelt. All these are really useful tools to have to know about, however, like all tools you can use them using good patterns and bad patterns. My tip is to always use constructor injection rather than property injection. You can learn why here. Happy Coding 🤘