1 (905) 264-1100 Monday-Friday, 9am - 6pm
info@apiqu.com Drop us a line anytime!
100 Arbors Lane, Unit D Vaughan, Canada
Toggle Menu
1 (905) 264-1100 Monday-Friday, 9am - 6pm
info@apiqu.com Drop us a line anytime!
100 Arbors Lane, Unit D Vaughan, Canada
Toggle Menu
Sitecore Experience Platform

Making Simple Injector and Sitecore Friends

Introduction

Each time you are beginning a new sitecore project it eventually comes to the decision of what exact container to use in order to store and resolve all of your dependencies. There are dozens of .NET containers you can find in Nuget and the most popular of them are: Microsoft Unity, Autofac, Ninject and others. Most developers are familiar with this names I guess from the beginning of 2008 and frankly speaking since the beginning of IoC and DI era there weren’t some radical changes, just syntax sugar and some perfomance improvements.

I was finishing my third cup of coffee when I started thinking about some new and fresh solutions to integrate with sitecore. After some time of googling I was found very interesting article about comparison all .NET containers by Daniel Palme, which introduces a couple of new generation containers such as DryIoc and Simple Injector. Both of them are easy-to-use, have fantastic performance and provide completely new functionality that old-school containers don’t have. Additionally, highly detailed documentation is a huge plus.

Integration

So here is a small guide on how I integrated Simple Injector into Sitecore using pipelines. Since Sitecore pipeline based solution the main idea is to be able to inject dependencies using pipeline architecture.

First step is to create main pipeline and pipeline custom arguments to pass our container through the pipeline group:

 


public class RegisterDependenciesPipelineArgs: PipelineArgs
{
    public virtual Container Container { get; set; }
}

public class RegisterDependeciesPipeline: IPipeline
{
    private const string RegisterDependencyPipelineName = "RegisterDependencies";

    #region Implementation of IPipeline

    public void Process(PipelineArgs args)
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
        container.Options.AllowOverridingRegistrations = true;

        var registerDependencyPipelineArgs = new RegisterDependenciesPipelineArgs {Container = container};
        var results = PipelineManager.Run(RegisterDependencyPipelineName, registerDependencyPipelineArgs);

        args = results;
        container.Verify();
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    }

    #endregion
        
}

So now we have a pipeline which creates the container and pass it through the pipeline group using our custom pipeline arguments. Let’s create a pipeline which will register our dependencies.


public class RegisterRepositoriesPipeline: IPipeline{
    #region Implementation of IPipeline

    public virtual void Process(RegisterDependenciesPipelineArgs args)
    {
        var container = args.Container;
        container.Register(typeof(IRepository), typeof(Repository), Lifestyle.Scoped);
    }

    #endregion
}

Alright, so everything is ready except config file, so let’s create one.


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      
      <initialize>
        <processor type="Web.Pipelines.Initialize.RegisterDependeciesPipeline, Web" />
      </initialize>

      <RegisterDependencies>
        <processor type="Kernel.Pipelines.RegisterRepositoriesPipeline, Kernel" />
      </RegisterDependencies>
      
    </pipelines>
  </sitecore>
</configuration>

And that’s it! Now if you want to add any other dependencies from separate assemblies just create another pipeline and add it to the <RegisterDependencies> pipeline group.

Summary

Dependency Container is definitely a strategic part of each solution. Nevertheless, most widely used Containers dramatically affects performance and don't support last trendy/handy Features. Personally for me, Simple Injector Container is number one especially for enterprise grade Sitecore solutions.