Thank you
var membershipProvider = DependencyResolver.Current .Resolve()
There are other methods. This article: https://cardinalcore.co.uk/2014/07/02/sitecore-pipelines-commands-using-ioc-containers /Use the container factory class to resolve the dependencies in the pipeline. This is the class used:
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);
}
}
Then this will be set to use the configuration The factory attribute in the event or processor factory. Example configuration:
< br />
master
factory>
Using the second method, you can inject dependencies in the constructor as usual.
These are probably the two most commonly used options.
I use Sitecore 8.1 MVC and Autofac as DI. I want to know how to inject resolved objects into objects created by Sitecore What is the recommended method, such as pipelines, commands, calculated fields, etc… For example, I am using a membership provider, and I need to call my business layer. Can I define a constructor on the class, will sitecore inject objects?
Thank you
Pipe handlers, commands, etc… basically anything that Sitecore creates-you are quite limited The usual method is to use the service locator pattern to resolve dependencies:
var membershipProvider = DependencyResolver.Current.Resolve()
< p>There are other ways. This article: https://cardinalcore.co.uk/2014/07/02/sitecore-pipelines-commands-using-ioc-containers/ Use container factory classes to resolve dependencies in pipelines .This is the class used:
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);
ret urn this.containerManager.Resolve(type);
}
}
Then, this will be set as the event or handler factory using the factory attribute in the configuration. Example configuration :
master
Using the second method, you can inject dependencies in the constructor as usual.
These are probably the two most commonly used options.