Create a custom synchronization strategy
Version: 10.3
To use a custom synchronization strategy:
-
Create a new custom strategy class and implement the
ISynchronizationStrategyinterface.This interface contains one
Resolvemethod that receives the direction of synchronization and a base product entity from the external system and Sitecore.The
Resolvemethod decides which system to update ( theECS or Sitecore) and returns the result.RequestResponsenamespace 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); } } -
Register a custom synchronization strategy class in the
Sitecore.Commerce.Products.configfile.To do this, change the type attribute value of the
synchronizationStrategyelement 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;
}
}