ISimpleSyncProviderConcurrencyConflictResolver 接口

命名空间: Microsoft.Synchronization.SimpleProviders
程序集: Microsoft.Synchronization.SimpleProviders(在 microsoft.synchronization.simpleproviders.dll 中)

语法

声明
Public Interface ISimpleSyncProviderConcurrencyConflictResolver
用法
Dim instance As ISimpleSyncProviderConcurrencyConflictResolver
public interface ISimpleSyncProviderConcurrencyConflictResolver
public interface class ISimpleSyncProviderConcurrencyConflictResolver
public interface ISimpleSyncProviderConcurrencyConflictResolver
public interface ISimpleSyncProviderConcurrencyConflictResolver

备注

有关并发冲突的更多信息,请参见为简单提供程序处理冲突

示例

在这个示例中,用于并发冲突和约束冲突的冲突解决策略作为 ApplicationDefined 的默认设置保留。这意味着应用程序将注册 ItemConflictingItemConstraint 事件,并且指定在同步处理期间发生冲突时用于解决冲突的操作。有关更多信息,请参见为简单提供程序处理冲突。若要在完整应用程序的上下文中查看此代码,请参见"Sync101 using Simple Sync Provider" 应用程序(可从 Sync Framework SDK 和 Code Gallery 获得)。下面的代码示例显示在 MyFullEnumerationSimpleSyncProvider 的构造函数中指定的事件处理程序。

this.ItemConstraint += new EventHandler<SimpleSyncItemConstraintEventArgs>(OnItemConstraint);
this.ItemConflicting += new EventHandler<SimpleSyncItemConflictingEventArgs>(OnItemConflicting);
AddHandler Me.ItemConstraint, AddressOf HandleItemConstraint

下面的代码示例显示将冲突解决操作设置为 Merge 的事件处理程序。

void OnItemConstraint(object sender, SimpleSyncItemConstraintEventArgs e)
{
    // Set the resolution action for constraint conflicts.
    // In this sample, the provider checks for duplicates in InsertItem, and this event would
    // fire if a duplicate occurred. 
    e.SetResolutionAction(ConstraintConflictResolutionAction.Merge);
}

void OnItemConflicting(object sender, SimpleSyncItemConflictingEventArgs e)
{
    // Set the resolution action for concurrency conflicts.
    e.SetResolutionAction(ConflictResolutionAction.Merge);
}
Private Sub HandleItemConstraint(ByVal sender As Object, ByVal e As SimpleSyncItemConstraintEventArgs)
    ' Set the resolution action for constraint conflicts. 
    ' In this sample, the provider checks for duplicates in InsertItem, and this event would 
    ' fire if a duplicate occurred. 
    e.SetResolutionAction(ConstraintConflictResolutionAction.Merge)
End Sub

Private Sub HandleItemConflicting(ByVal sender As Object, ByVal e As SimpleSyncItemConflictingEventArgs)
    ' Set the resolution action for concurrency conflicts. 
    e.SetResolutionAction(ConflictResolutionAction.Merge)
End Sub

下面的代码示例显示 ResolveUpdateUpdateConflict 方法,该方法是为响应并发冲突的合并冲突解决操作而实现的:

public void ResolveUpdateUpdateConflict(object itemData, 
    IEnumerable<SyncId> changeUnitsToMerge, 
    IEnumerable<SyncId> changeUnitsToUpdate, 
    ItemFieldDictionary keyAndExpectedVersion, 
    RecoverableErrorReportingContext recoverableErrorReportingContext, 
    out ItemFieldDictionary updatedVersion)
{
    ItemTransfer transfer = (ItemTransfer)itemData;
    ItemData dataCopy = new ItemData(transfer.ItemData);

    // Combine the conflicting data.
    ItemData mergedData = (_store.Get(transfer.Id)).Merge((ItemData)dataCopy);
    ulong timeStamp = _store.UpdateItem(transfer.Id, mergedData);

    updatedVersion = new ItemFieldDictionary();
    updatedVersion.Add(new ItemField(CUSTOM_FIELD_TIMESTAMP, typeof(ulong), timeStamp));
}
Public Sub ResolveUpdateUpdateConflict(ByVal itemData As Object, ByVal changeUnitsToMerge As IEnumerable(Of SyncId), ByVal changeUnitsToUpdate As IEnumerable(Of SyncId), ByVal keyAndExpectedVersion As ItemFieldDictionary, ByVal recoverableErrorReportingContext As RecoverableErrorReportingContext, _
    ByRef updatedVersion As ItemFieldDictionary) Implements ISimpleSyncProviderConcurrencyConflictResolver.ResolveUpdateUpdateConflict
    Dim transfer As ItemTransfer = DirectCast(itemData, ItemTransfer)
    Dim dataCopy As New ItemData(transfer.ItemData)

    ' Combine the conflicting data. 
    Dim mergedData As ItemData = (_store.[Get](transfer.Id)).Merge(DirectCast(dataCopy, ItemData))
    Dim timeStamp As ULong = _store.UpdateItem(transfer.Id, mergedData)

    updatedVersion = New ItemFieldDictionary()
    updatedVersion.Add(New ItemField(CUSTOM_FIELD_TIMESTAMP, GetType(ULong), timeStamp))
End Sub

请参阅

参考

ISimpleSyncProviderConcurrencyConflictResolver 成员
Microsoft.Synchronization.SimpleProviders 命名空间