You are here: Advanced Replication Strategies > Dealing with Conflicts

Dealing with Conflicts

When replicated objects are changed independent and the replicated again, conflict are inevitable. The default behavior of dRS is to throw a ReplicationConflictException in such a situation. This prevents corruption, but doesn't help to resolve the conflict. Fortunately dRS allows you to handle such a conflict situation properly.

To deal with conflicts you use a event listener. See "Events" . The event has also information about conflicts. When a change conflicts with another change, the conflict flag is set on the replication event. Then you can decide which state is actually replicated. For example: When a conflict occurs you could always take the state of one replication partner.

class SimpleConflictResolvingListener : IReplicationEventListener
{
    public void OnReplicate(IReplicationEvent replicationEvent)
    {
        if (replicationEvent.IsConflict())
        {
            IObjectState stateOfTheDesktop = replicationEvent.StateInProviderA();
            replicationEvent.OverrideWith(stateOfTheDesktop);
        }
    }
}
AdvancedReplicationExamples.cs: Conflict resolving listener
Private Class SimpleConflictResolvingListener
    Implements IReplicationEventListener
    Public Sub OnReplicate(ByVal replicationEvent As IReplicationEvent) _
                                     Implements IReplicationEventListener.OnReplicate
        If replicationEvent.IsConflict() Then
            Dim stateOfTheDesktop As IObjectState = replicationEvent.StateInProviderA()
            replicationEvent.OverrideWith(stateOfTheDesktop)
        End If
    End Sub
End Class
AdvancedReplicationExamples.vb: Conflict resolving listener
IReplicationSession replicationSession =
    Replication.Begin(desktopRelicationPartner, mobileRelicationPartner,
                        new SimpleConflictResolvingListener());
AdvancedReplicationExamples.cs: Deal with conflicts
Dim replicationListener As IReplicationEventListener = New SimpleConflictResolvingListener()

Dim replicationSession As IReplicationSession _
    = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)
AdvancedReplicationExamples.vb: Deal with conflicts

Of course there are more advanced strategies possible. For example can compare the timestamp's of the conflicting changes and take the newer change. Note that the clocks of disconnected devices are not perfectly synchronized.

class TakeLatestModificationOnConflictListener : IReplicationEventListener
{
    public void OnReplicate(IReplicationEvent replicationEvent)
    {
        if (replicationEvent.IsConflict())
        {
            IObjectState stateOfTheDesktop = replicationEvent.StateInProviderA();
            IObjectState stateOfTheMobile = replicationEvent.StateInProviderB();

            if (stateOfTheDesktop.ModificationDate() >= stateOfTheMobile.ModificationDate())
            {
                replicationEvent.OverrideWith(stateOfTheDesktop);
            }
            else
            {
                replicationEvent.OverrideWith(stateOfTheMobile);
            }
        }
    }
}
AdvancedReplicationExamples.cs: Listener which takes latest changes
Private Class TakeLatestModificationOnConflictListener
    Implements IReplicationEventListener
    Public Sub OnReplicate(ByVal replicationEvent As IReplicationEvent) _
                            Implements IReplicationEventListener.OnReplicate
        If replicationEvent.IsConflict() Then
            Dim stateOfTheDesktop As IObjectState = replicationEvent.StateInProviderA()
            Dim stateOfTheMobile As IObjectState = replicationEvent.StateInProviderB()

            If stateOfTheDesktop.ModificationDate() >= stateOfTheMobile.ModificationDate() Then
                replicationEvent.OverrideWith(stateOfTheDesktop)
            Else
                replicationEvent.OverrideWith(stateOfTheMobile)
            End If
        End If
    End Sub
End Class
AdvancedReplicationExamples.vb: Listener which takes latest changes
IReplicationSession replicationSession =
    Replication.Begin(desktopRelicationPartner, mobileRelicationPartner,
                       new TakeLatestModificationOnConflictListener());
AdvancedReplicationExamples.cs: Take latest change
Dim replicationListener As IReplicationEventListener = _
    New TakeLatestModificationOnConflictListener()

Dim replicationSession As IReplicationSession _
    = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)
AdvancedReplicationExamples.vb: Take latest change