Replacing Sitecore’s default DI Container by Autofac

This note is made while I’m experimenting on Sitecore and Helix principles, also get a reference from Habitat.

If you’ve had a chance to take a view at Habitat, they’ve already implemented this module – Sitecore.Foundation.DependencyInjection. It’s using the native Dependency Injection of Sitecore which is based on the Microsoft.Extensions.DependencyInjection from ASP.NET Core. This implementation has an extension for ServiceCollection to automatically register all Controller and Services. This extension class can be found at – ServiceCollectionExtensions.cs.

My aim is trying to experiment on replacing service provider which is provided from Sitecore’s Dependency Injection document. And I choose Autofac as a target due to some personal reasons.

Sitecore Documentation_ Dependency injection

There is an article from Dmytro Shevchenko, it is – Replacing the Default DI Container in Sitecore 8.2. In which, he provides 2 ways of implementation, but I prefer the 2nd way – “Replacing the DI implementation via <serviceProviderBuilder> because it’s Sitecore way. The post provided enough information, but, certainly, there are some stuck that could get several hours from you to understand what it is and fix it. Such as the Nuget’s version of Autofac and its extensions; and you may also meet the question of how can it’s applied to the project that based on Helix principle. Along with this post, I’ll show you the details of my implementation.

An AutofacDependencyInjection module

1- Firstly, I’ve supposed we already had a blank module – AutofacDependencyInjection under Foundation layer.

2- Then, install the following libraries from Nuget

  • Autofac (latest version – 4.0.8 as current May 2018)
  • Autofac.Extensions.DependencyInjection (latest version – 4.2.2 as current May 2018)
  • Autofac.Mvc5 (latest version – 4.0.2 as current May 2018)
  • Microsoft.Extensions.DependencyInjection.Abstractions (version 1.1.1)
  • Microsoft.Extensions.DependencyInjection (version 1.1.1)

3- Next, We have to create our own Service Provider Builder. These are 2 notice parts in this step.

AutofacServiceProviderBuilder

3.1. First, we have to follow Sitecore’s guide on replacing Default Service Provider

[+] Let’s name this class is – AutofacServiceProviderBuilder.

[+] This class is inherited from Sitecore.DependencyInjection.BaseServiceProviderBuilder

[+] We need to override BuildServiceProvider method.

3.2. Second, we use Autofac’s functionalities

[+] builder.Populate(serviceCollection) – Just quoted from the article Replacing the Default DI Container in Sitecore 8.2, because this author gives fully enough explanation for us, I think.

builder-populate-serviceCollection

[+] Register all Controllers from entire layers, in my case these are Core (which is Foundation term) and Features layers.

[+] Register all Autofac’s modules which are declared inside our modules (in term of Helix’s module). We can imagine those are similar to Configurator (see a screenshot below) of Sitecore’s Dependency Injection; but the advantages from Autofac brings to us is we don’t need to declare the configurator through Sitecore’s patch files. Instead, Autofac has a method to collect all modules registration from assemblies argument. I’ll show the way of registering services via Autofac’s module in below. 

Sitecore's configurator

 

4- Next, It needs to be updated for runtime assembly binding in web.config. It’s applied for the following assemblies

  • Autofac
  • Microsoft.Extensions.DependencyInjection.Abstractions
  • Microsoft.Extensions.DependencyInjection

update-runtime-assemblyBinding

5- Eventually, it’s also the last step from Sitecore’s guidance which will replace the default builder in Sitecore.config. But we don’t directly change Sitecore.config file, instead, we use the patch file to accomplish it. It looks like

patch-replace-service-provider

 

Yay, let consume it

These are 2 ways for services registration. One is via XML patch config, other is via Autofac’s module. If there is no special just use XML patch config; for services that require complex to instantiate, the Autofac’s module should be used.

  1. Services registration through XML patch config

service-registration-via-patch

  1. Services registration through Autofac’s module
  • First of all, it needs to be installed Autofac (version 4.0.8 latest as current May 2018) via Nuget for relative modules.
  • Then create our own ModuleRegistration which is inherited from Autofac.Module; then register service’s contract and its implementation by overriding its Load method. For example:

service-registration-via-autofac-module

 

Let’s see how it works!!!!! 🙂

Some resources that I’ve referred

  1. Habitat Dependency Injection with Sitecore 8.2
  2. Dependency Injection in Sitecore 8.2 (from Kam Figy – the author of Unicorn)
  3. Replacing the Default DI Container in Sitecore 8.2
  4. Document of Sitecore’s Dependency Injection

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s