Sitecore

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

Sitecore - Understanding Development Approaches: A Sitecore Outlook
Understanding Development Approaches: A Sitecore Outlook

Designing, building, and implementing top-notch experiences not only requires a great deal of planning, strategy, and time – it also requires the right digital experience platform (DXP) and the right development approach for your business needs.

Get the Guide

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.AddTransient<MySampleController>();
            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="http://www.sitecore.net/xmlconfig/">
  <sitecore>    
    <services>
      <configurator type="MySampleProject.IoC.MySampleConfigurator, MySampleProject" />
    </services>    
  </sitecore>
</configuration>

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.

About the Author

More from this Author

Leave a Reply

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

Subscribe to the Weekly Blog Digest:

Sign Up
Categories