Implement a custom ID generator

The ID generator takes a unique external ID in the form of a string and returns a unique ID, which can be used as an item ID (for example, GUID). The default implementation is based on the MD5 hash algorithm.

Note

Sitecore IDs must be unique and because the output from the ID generator is used as a Sitecore ID, it is important to always prefix the unique external ID with an arbitrary but fixed string, in order to avoid collision. For example, the IDs for manufacturers in the external system repository might have overlap with the IDs for the products, even though they are unique within their own range. The input to the ID generator must therefore be in the following format: Manufacturer + ManufacturerID and Products + ProductID respectively.

To use a custom ID generator:

  1. Create a new ID Generator class and implement the IIdGenerator interface.

    The interface has one StringToID method that accepts two parameters:

    • A string containing the value of the external ID of the given entity.

    • A string containing a unique prefix to avoid collision between identical values used for different entities.

      The method returns a GUID as result, which is used to assign to the corresponding item in Sitecore representing the external entity.

    /// <summary>
    /// Defines interface for id generator.
    /// </summary>
    public interface IIdGenerator
    {
        /// <summary>
        /// String to Sitecore ID.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="prefix">The prefix.</param>
        /// <returns>The generated ID</returns>
        [NotNull]
        ID StringToID([NotNull] string value, [NotNull] string prefix);
    }
  2. Register the custom ID Generator class in the Sitecore.Commerce.Products.config file.

    To do this, change type attribute value of  the idGenerator element to the custom ID Generator type.

    <idGenerator type="Sitecore.Commerce.Data.Products.Md5IdGenerator, Sitecore.Commerce" singleInstance="true" />
    The default implementation is based on the MD5 hash algorithm provided in .NET. The source code is listed below:
    /// <summary>
    /// Defines default implementation of id generator.
    /// </summary>
    public class Md5IdGenerator : IIdGenerator
    {
        /// <summary>
        /// String to Sitecore ID.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="prefix">The prefix.</param>
        /// <returns>The generated ID</returns>
    
        public ID StringToID(string value, string prefix)
        {
            Assert.ArgumentNotNull(value, "value");
            Assert.ArgumentNotNull(prefix, "prefix");
            // Create a new instance of the MD5CryptoServiceProvider object.
            var md5Hasher = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(prefix + value));
            return new ID(new Guid(data));
        }
    }