Sitecore: enable sitecore component dependency

I am using Sitecore 8.1 MVC with Autofac as DI. I was wondering how it is recommended to embed the allowed objects in the created sitecore objects, i.e. Conveyors, commands, computed fields, etc. As an example, I use a membership provider, in which I need to call my business level. Is it possible to define a class constructor and sitecore will inject objects?

thank

+4
source share
1 answer

With things like pipelined processors, instructions, etc. Basically, everything that Sitecore creates is pretty limited. The usual approach is to use the Locator pattern to define dependencies:

var membershipProvider = DependencyResolver.Current.Resolve<IMembershipProvider>()

. : https://cardinalcore.co.uk/2014/07/02/sitecore-pipelines-commands-using-ioc-containers/ factory . :

using System;
using System.Diagnostics.CodeAnalysis;

using Sitecore.Reflection;

public class ContainerFactory : IFactory
{
    private readonly IContainerManager containerManager;

    public ContainerFactory() : this(new LocatorContainerManager()) // service locate an appropriate container
    {
    }

    public ContainerFactory(IContainerManager containerManager)
    {
        this.containerManager = containerManager;
    }

    public object GetObject(string identifier)
    {
        Type type = Type.GetType(identifier);
        return this.containerManager.Resolve(type);
    }
}

factory , factory . config:

<sitecore>
  <events>
    <event name="item:saved">
      <handler factory="ContainerFactory" ref="MyApp.MyHandler, MyApp" method="MyMethod">
        <database>master</database>
      </handler>
    </event>
  </events>
  <pipelines>
    <MyPipeline>
      <processor type="1" factory="ContainerFactory" ref="MyApp.MyProcessor, MyApp" />
    </MyPipeline>
  </pipelines>
  <factories>
    <factory id="ContainerFactory" type="MyApp.ContainerFactory"></factory>
  </factories>
</sitecore>

, .

, , 2 .

+3

All Articles