Create a custom synchronization strategy

Version: 10.3

To use a custom synchronization strategy:

  1. Create a new custom strategy class and implement the ISynchronizationStrategy interface.

    This interface contains one Resolve method that receives the direction of synchronization and a base product entity from the external system and Sitecore.

    The Resolve method decides which system to update ( theECS or Sitecore) and returns the result.

    RequestResponse
    namespace Sitecore.Commerce.Products
    {
        using Sitecore.Commerce.Entities.Products;
    
        /// <summary>
        /// The SynchronizationStrategy interface.
        /// </summary>
    
        public interface ISynchronizationStrategy
        {
            /// <summary>
            /// Resolves the specified direction.
            /// </summary>
            /// <param name="direction">The direction.</param>
            /// <param name="externalSystemEntity">The external system entity.</param>
            /// <param name="sitecoreEntity">The entity from content management system.</param>
            /// <returns>The place, where we decide if entity is updated.</returns>
    
            UpdateIn Resolve(Direction direction, ProductEntity externalSystemEntity, ProductEntity sitecoreEntity);
        }
    }
  2. Register a custom synchronization strategy class in the Sitecore.Commerce.Products.config file.

    To do this, change the type attribute value of the synchronizationStrategy element to custom synchronization strategy type.

RequestResponse
<synchronizationStrategy type="Sitecore.Commerce.Products.DateTimeSynchronizationStrategy, Sitecore.Commerce" singleInstance="true" />

The default DateTime based strategy that comes with Connect is the simplest possible strategy:

/// <summary>

/// The synchronization strategy based on updated date of entity in external system and content management system.

/// </summary>

public class DateTimeSynchronizationStrategy : ISynchronizationStrategy

{

    /// <summary>
    /// Resolves the specified direction.
    /// </summary>
    /// <param name="direction">The direction.</param>
    /// <param name="externalSystemEntity">The external system entity.</param>
    /// <param name="sitecoreEntity">The entity from content management system.</param>
    /// <returns>
    /// The place, where we decide if entity is updated.
    /// </returns>

    public UpdateIn Resolve(Direction direction, ProductEntity externalSystemEntity, ProductEntity sitecoreEntity)
    {
        if (string.IsNullOrEmpty(sitecoreEntity.ExternalId) && (direction == Direction.Both || direction == Direction.Inbound))
        {
            return UpdateIn.Sitecore;
        }
        if (externalSystemEntity.Updated == sitecoreEntity.Updated)
        {
            return UpdateIn.None;
        }
        if (externalSystemEntity.Updated < sitecoreEntity.Updated)
        {
            if (direction == Direction.Both || direction == Direction.Outbound)
            {
                return UpdateIn.ExternalCommerceSystem;
            }
        }
        else
        {
            if (direction == Direction.Both || direction == Direction.Inbound)
            {
                return UpdateIn.Sitecore;
            }
        }
        return UpdateIn.None;
    }
}

Do you have some feedback for us?

If you have suggestions for improving this article,