Autofac as DI container in Sitecore Helix architecture

The following article describe how to use Autofac as DI container in Sitecore application based on Helix architecture. As you must be knowing in Helix architecture the whole application functionality is divided into multiple features with each feature being an independent functionality not dependent on other features. This article is based on the article written by Kevin Brechbühl at https://ctor.io/one-way-to-implement-dependency-injection-for-sitecore-habitat/

The only difference is that instead of creating processes in individual feature project I will be registering all the dependency at one place using the module feature of autofac.

To start with we will create a project in foundation layer with name “ProjectName.Foundation.DependencyInjection” . I have given my project name as Piccolo and hence the project name will be “Piccolo.Foundation.DependencyInjection

Follow the following steps after creating the project

  1. Add nugget package for Autofac to the project. Command line for nugget is as below: Install-Package Autofac.Mvc5 -Version 4.0.2
  2. We will be creating a custom pipeline in order to set autofac as dependency injection container in Sitecore pipeline. Add folder named Pipelines in the “Piccolo.Foundation.DependencyInjection”
  3. Inside Pipelines add another folder InitializeContainer and within InitializeContainer add a class InitializeContainer.cs.
  4. Add another folder Foundation to the project “Piccolo.Foundation.DependencyInjection” and add config file Foundation.DependencyInection.config to it.

The project should look like as below once you have finished the above steps.

autofac-sitecore-project

Populate the class InitializeContainer with following listing

public class InitializeContainer

{

public void Process(PipelineArgs args)

{

var builder = new ContainerBuilder();

// Register dependencies in controllers

var assembliesInAppDomain = AppDomain.CurrentDomain.GetAssemblies().ToArray();

builder.RegisterControllers(assembliesInAppDomain);

// Register dependencies in filter attributes

builder.RegisterFilterProvider();

// Register dependencies in custom views

builder.RegisterSource(new ViewRegistrationSource());

builder.RegisterAssemblyModules(assembliesInAppDomain);

var container = builder.Build();

// Set MVC DI resolver to use our Autofac container

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

} }

As you can see in the listing, all the module and controller within the appdomain assemblies are registerd in the lines below

var assembliesInAppDomain = AppDomain.CurrentDomain.GetAssemblies().ToArray();

builder.RegisterControllers(assembliesInAppDomain);

builder.RegisterAssemblyModules(assembliesInAppDomain)

Add the following configuration in Foundation.DependencyInjection.config where we are adding our process just before sitecore calls its initialize controller factory


<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”&gt;

<sitecore>

<pipelines>

<initialize>

<processor type =”Piccolo.Foundation.DependencyInjection.Pipelines.InitializeContainer.InitializeContainer, Piccolo.Foundation.DependencyInjection”

patch:before=”processor[@type=’Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc’]” />

</initialize>

<initializeContainer>

</initializeContainer>

</pipelines>

</sitecore>

</configuration>

Now in individual Feature modules we need to create autofac module which will automatically be picked up by our sitecore custom pipleline.

For illustration, I will take one feature as example. The name of feature project is Piccolo.Feature.Gallery

Add a class GalleryModule to the project inherited from module where you will register all the dependencies for corresponding feature


public class GalleryModule:Module

{

protected override void Load(ContainerBuilder builder)

{

builder.RegisterType().As<IRepository>();

}

}

For each feature you can create a module in same way.

Now the GalleryController in the above feature expects a constructor with parameter IRepository which will be provided by autofac.

public class GalleryController : Controller

{

private IRepository _imageArticleRepository;

public GalleryController(IRepository imageArticleRepository)

{

_imageArticleRepository = imageArticleRepository;

}

// GET: Gallery

public ActionResult Search(string searchTerm = “”)

{

var rootPath = Sitecore.Context.Database.GetItem(“/sitecore/content/Home/Album”);

var result = _imageArticleRepository.Get(rootPath.ID);

return View(result);

}

}

Autofac will automatically inject the dependency in to constructor into the controller which will then be available for use in the controller.

Advertisements