Belangrijke mededeling over deze pagina:

Deze pagina is niet beschikbaar in het Nederlands en daarom wordt de Engelse versie van het artikel getoond. Dit komt vooral voor bij technische artikelen.

Following up on my previous blog post about Isolating Sitecore pipeline processors and event handlers, I'd also like to share this more generic way of making pipeline processors and event handlers configurable.
This abstract class allows you to configure any processor using XML elements.

First of all, let's take a look at the configuration for the processor.
<config> is the root element for our configuration and may be any name. I've used config because it best describes its purpose.
hint="raw:Config" tells Sitecore to use the Config method on the processor when it's being constructed to process the XML.
Sitecore will then pass each of the child nodes of <config> as an XmlNode object to the Config method.

<pipelines>
  <examplePipeline>
    <processor type="ParTech.Pipelines.Example, ParTech">
      <config hint="raw:Config">
        <param1>value 1</param1>
        <param2>value 2</param2>
        <use_any_name>value 3</use_any_name>
      </config>
    </processor>
  </examplePipeline>
<pipelines>

Now let's look at the code for the ConfigurableProcessor base class.
The Config method is called for every child within the <config> node.

using System.Collections.Generic;
using System.Xml;

/// <summary>
/// Configurable pipeline processor.
/// </summary>
public abstract class ConfigurableProcessor
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ConfigurableProcessor"/> class.
    /// </summary>
    protected ConfigurableProcessor()
    {
        this.Configuration = new Dictionary<string, string>();
    }

    /// <summary>
    /// Gets the processor configuration.
    /// </summary>
    protected Dictionary<string, string> Configuration { get; private set; }

    /// <summary>
    /// Configures the processor.
    /// </summary>
    /// <param name="node">The node.</param>
    public void Config(XmlNode node)
    {
        this.Configuration.Add(node.Name, node.InnerText);
    }
}

Here is an example of an implementation of the base class.
All the configured parameters are easily accesible through the Configuration dictionary:

public class Example : ConfigurableProcessor
{
    public void Process(PipelineArgs args)
    {
        string param1 = this.Configuration["param1"];       // returns "value 1"
        string param2 = this.Configuration["param2"];       // returns "value 2"
        string param3 = this.Configuration["use_any_name"]; // returns "value 3"
    }
}

Event handlers

The exact same method works for event handlers too!
Just use the same base class and configure the event handler like this:

<event name="myevent">
  <handler type="ParTech.Events.Example, ParTech" method="OnExampleMethod">
    <config hint="raw:Config">
      <param1>value 1</param1>
      <param2>value 2</param2>
    </config>
  </handler>
</event>

Using Lists

Sitecore offers a few other possibilities when it comes to this type of configuration.
You can use hint="list" to automatically map the configured values to a string collection in your code:

<config hint="list">
  <item>value 1</item> <!-- "item" may be any name -->
  <item>value 2</item>
</config>
public class MyProcessorOrEvent
{
    public MyProcessorOrEvent()
    {
        this.Config = new List<string>();
    }
    
    // Contains: "value 1", "value 2"
    public List<string> Config { get; private set; }
}

You can also use hint="list:MethodName" to let Sitecore call a method (in this case MethodName) that handles the addition of the list value:

<config hint="list:Add">
  <item>value 1</item> <!-- "item" may be any name -->
  <item>value 2</item>
</config>
public class MyProcessorOrEvent
{
    public MyProcessorOrEvent()
    {
        this.Config = new List<string>();
    }
    
    public List<string> Config { get; private set; }
    
    public void Add(string item)
    {
        this.Config.Add(item);
    }
}

Plaats een reactie