---
title: "カスタム同期戦略を作成する"
slug: "create-a-custom-synchronization-strategy"
abstract: ""
---
# カスタム同期戦略を作成する

カスタム同期戦略を使うには:

1.  新しいカスタムストラテジークラスを作成し、ISynchronizationStrategyインターフェースを実装します。
    
    このインターフェースには、同期の方向を受け入れるResolveメソッドと、外部システムおよびSitecoreからの基本プロダクトエンティティが含まれています。
    
    Resolveメソッドはどのシステム(theECSかSitecore)を更新するかを決定し、その結果を返します。
    
    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.  Sitecore.Commerce.Products.configファイルにカスタム同期戦略クラスを登録します。
    
    これを行うには、synchronizationStrategy要素のタイプ属性値をカスタム同期戦略タイプに変更します。
    

<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;
 }
}