Skip to main content


Sitecore Upgrades: A Mini-Series Part 5 Dependency Injection Changes

The final post in my mini-series on Sitecore upgrades centers around dependency injection. Your solution may be using a popular third party tool like Castle Windsor or Simple Injector to handle DI (dependency injection). During the upgrade process, I recommend removing your dependency on these third party tools and implementing DI using .NET’s dependency injector. After reading my previous blog post in this series, you should be noticing a pattern in my approach. Reducing your dependency on third party tools will make your code base more self-sufficient and easier to upgrade.

How to get started

Once again navigate to the NuGet Package Manager for your Solution. You will need to install the appropriate versions of the Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.DependencyInjection.Abstractions packages. Install these packages with the Dependency Behavior set to Lowest to the appropriate projects.

Implementing .NET DI in a single project

Search through all of the constructors in your project and take note of all the services required by these classes.

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;

namespace MySampleProject.IoC
    public class MySampleConfigurator : IServicesConfigurator
        public void Configure(IServiceCollection serviceCollection)
            serviceCollection.AddScoped<IMySampleSearchService, MySampleSearchService>();         

You will need to register each controller in the project individually. You will also need to register any services required by your classes here as well. When registering a service, you are presented with three options: registering the service as a transient service, a scoped service, or a singleton service. These three options define the lifetimes of the services, or in other words, how long these services are accessible by your code. For more information on how to decide what lifetime you need your service to have, head over to the .NET dependency injection documentation here.

After you have registered your services, you will need to create a patch file to inform Sitecore that you are injecting these services. Your patch file will look like this:

<configuration xmlns:patch="">
      <configurator type="MySampleProject.IoC.MySampleConfigurator, MySampleProject" />

Each required service should only be registered ONCE in your solution. If a service is widely used across multiple projects, you will not be registering it in each project individually. You will need to register it in a more foundational layer to prevent re-registration. After successfully updating DI for a single project, you will need to repeat these steps for all the necessary projects throughout your solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Zach Gay, Senior Technical Consultant, Perficient's Sitecore practice

More from this Author

Follow Us