Dependent injection – Sitecore: will depend on the SiteCore component

I use Sitecore 8.1 MVC and Autofac as DI. I want to know what is the recommended way to inject parsed objects into objects created by Sitecore, 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

pipeline processor, command, etc… Basically anything that Sitecore creates-you are pretty limited. The usual way is to use the service locator pattern to resolve dependencies:

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:





master
< br />










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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2331 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.