次の方法で共有


INotifyingChangeApplierTarget.SaveChangeWithChangeUnits メソッド

派生クラスでオーバーライドされると、変更単位の変更を含む項目変更を、項目ストアに保存します。

名前空間: Microsoft.Synchronization
アセンブリ: Microsoft.Synchronization (microsoft.synchronization.dll 内)

構文

'宣言
Sub SaveChangeWithChangeUnits ( _
    change As ItemChange, _
    context As SaveChangeWithChangeUnitsContext _
)
'使用
Dim instance As INotifyingChangeApplierTarget
Dim change As ItemChange
Dim context As SaveChangeWithChangeUnitsContext

instance.SaveChangeWithChangeUnits(change, context)
void SaveChangeWithChangeUnits (
    ItemChange change,
    SaveChangeWithChangeUnitsContext context
)
void SaveChangeWithChangeUnits (
    ItemChange^ change, 
    SaveChangeWithChangeUnitsContext^ context
)
void SaveChangeWithChangeUnits (
    ItemChange change, 
    SaveChangeWithChangeUnitsContext context
)
function SaveChangeWithChangeUnits (
    change : ItemChange, 
    context : SaveChangeWithChangeUnitsContext
)

パラメーター

  • change
    適用する項目変更です。
  • context
    適用される変更に関する情報です。

解説

実装に関するメモ : context に含まれているアクションが Create である場合、項目変更はアトミックに保存する必要があります。つまり、すべての変更単位を正常に保存するか、項目変更全体を破棄する必要があります。変更単位の一部を保存できても、残りの変更単位の保存に失敗すると、データが破損する可能性があります。 RecordRecoverableErrorForChangeUnit が呼び出されたときに、context が Create である場合、RecordRecoverableErrorForChangeUnitCreateFailuresMustBeForEntireItemException をスローします。 このメソッドの実行中に同期が取り消された場合、プロバイダーによって変更単位の一部が適用済みであれば、プロバイダーは、適用されていない変更単位のすべてに対して RecordRecoverableErrorForChangeUnit を呼び出す必要があります。

次の例では、SaveChangeWithChangeUnits メソッド内で、特に一般的な SaveChangeAction 値の一部を処理しています。

Public Sub SaveChangeWithChangeUnits(ByVal change As ItemChange, ByVal context As SaveChangeWithChangeUnitsContext) Implements INotifyingChangeApplierTarget.SaveChangeWithChangeUnits
    ' Enumerate the change units received and apply them by using the specified action.
    For Each cuChange As ChangeUnitChange In change.ChangeUnitChanges
        Select Case context.GetActionForChangeUnit(cuChange)
            Case SaveChangeAction.Create, SaveChangeAction.UpdateVersionAndData
                If True Then
                    ' Update the item store and metadata store for the specified change unit.
                    Try
                        Dim cuData As String = DirectCast(context.ChangeData, String())(cuChange.ChangeUnitId.GetByteId())
                        _ContactStore.UpdateContactFromSync(change, cuChange, cuData)
                    Catch ex As Exception
                        Dim errData As New RecoverableErrorData(ex)
                        context.RecordRecoverableErrorForChangeUnit(cuChange, errData)
                    End Try
                    Exit Select
                End If
            Case SaveChangeAction.UpdateVersionAndMergeData
                If True Then
                    ' Merge actions are not supported by this implementation.
                    Throw New NotImplementedException("UpdateVersionAndMergeData is not supported.")
                End If
            Case SaveChangeAction.UpdateVersionOnly
                If True Then
                    ' Update only the version of this change unit in the metadata store.
                    Try
                        _ContactStore.UpdateContactVersion(change.ItemId, cuChange.ChangeUnitId, cuChange.ChangeUnitVersion)
                    Catch ex As Exception
                        Dim errData As New RecoverableErrorData(ex)
                        context.RecordRecoverableErrorForChangeUnit(cuChange, errData)
                    End Try
                    Exit Select
                End If
            Case SaveChangeAction.DeleteAndRemoveTombstone, SaveChangeAction.DeleteAndStoreTombstone
                If True Then
                    ' Delete actions are handled in SaveItemChange, so throw an exception.
                    Throw New InvalidOperationException("SaveChangeWithChangeUnits received a delete action.")
                End If
            Case Else
                If True Then
                    Throw New ArgumentOutOfRangeException("SaveChangeWithChangeUnits received an out-of-range action.")
                End If
        End Select
    Next

    ' Use the metadata storage service to save the knowledge as each change is applied. Saving knowledge as each change is applied is 
    ' not required. It is more robust than saving the knowledge only after each change batch, because if synchronization is interrupted 
    ' before the end of a change batch, the knowledge will still reflect all of the changes applied. However, it is less efficient because 
    ' knowledge must be stored more frequently.
    Dim updatedKnowledge As SyncKnowledge = Nothing
    Dim updatedForgottenKnowledge As ForgottenKnowledge = Nothing
    context.GetUpdatedDestinationKnowledge(updatedKnowledge, updatedForgottenKnowledge)
    _ContactStore.ContactReplicaMetadata.SetKnowledge(updatedKnowledge)
End Sub
public void SaveChangeWithChangeUnits(ItemChange change, SaveChangeWithChangeUnitsContext context)
{
    // Enumerate the change units received and apply them by using the specified action.
    foreach (ChangeUnitChange cuChange in change.ChangeUnitChanges)
    {
        switch (context.GetActionForChangeUnit(cuChange))
        {
            case SaveChangeAction.Create:
            case SaveChangeAction.UpdateVersionAndData:
            {
                // Update the item store and metadata store for the specified change unit.
                try
                {
                    string cuData = ((string[])context.ChangeData)[cuChange.ChangeUnitId.GetByteId()];
                    _ContactStore.UpdateContactFromSync(change, cuChange, cuData);
                }
                catch (Exception ex)
                {
                    RecoverableErrorData errData = new RecoverableErrorData(ex);
                    context.RecordRecoverableErrorForChangeUnit(cuChange, errData);
                }
                break;
            }
            case SaveChangeAction.UpdateVersionAndMergeData:
            {
                // Merge actions are not supported by this implementation.
                throw new NotImplementedException("UpdateVersionAndMergeData is not supported.");
            }
            case SaveChangeAction.UpdateVersionOnly:
            {
                // Update only the version of this change unit in the metadata store.
                try
                {
                    _ContactStore.UpdateContactVersion(change.ItemId, cuChange.ChangeUnitId, cuChange.ChangeUnitVersion);
                }
                catch (Exception ex)
                {
                    RecoverableErrorData errData = new RecoverableErrorData(ex);
                    context.RecordRecoverableErrorForChangeUnit(cuChange, errData);
                }
                break;
            }
            case SaveChangeAction.DeleteAndRemoveTombstone:
            case SaveChangeAction.DeleteAndStoreTombstone:
            {
                // Delete actions are handled in SaveItemChange, so throw an exception.
                throw new InvalidOperationException("SaveChangeWithChangeUnits received a delete action.");
            }
            default:
            {
                throw new ArgumentOutOfRangeException("SaveChangeWithChangeUnits received an out-of-range action.");
            }
        }
    }

    // Use the metadata storage service to save the knowledge as each change is applied. Saving knowledge as each change is applied is 
    // not required. It is more robust than saving the knowledge only after each change batch, because if synchronization is interrupted 
    // before the end of a change batch, the knowledge will still reflect all of the changes applied. However, it is less efficient because 
    // knowledge must be stored more frequently.
    SyncKnowledge updatedKnowledge;
    ForgottenKnowledge updatedForgottenKnowledge;
    context.GetUpdatedDestinationKnowledge(out updatedKnowledge, out updatedForgottenKnowledge);
    _ContactStore.ContactReplicaMetadata.SetKnowledge(updatedKnowledge);
}

参照

リファレンス

INotifyingChangeApplierTarget インターフェイス
INotifyingChangeApplierTarget メンバー
Microsoft.Synchronization 名前空間