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.
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.
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.
[+] 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.
4- Next, It needs to be updated for runtime assembly binding in web.config. It’s applied for the following assemblies
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
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.
- Services registration through XML patch config
- 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:
Let’s see how it works!!!!! 🙂
Some resources that I’ve referred
- Habitat Dependency Injection with Sitecore 8.2
- Dependency Injection in Sitecore 8.2 (from Kam Figy – the author of Unicorn)
- Replacing the Default DI Container in Sitecore 8.2
- Document of Sitecore’s Dependency Injection