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); } } }
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
IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner, new SimpleConflictResolvingListener());
Dim replicationListener As IReplicationEventListener = New SimpleConflictResolvingListener() Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)
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); } } } }
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
IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner, new TakeLatestModificationOnConflictListener());
Dim replicationListener As IReplicationEventListener = _ New TakeLatestModificationOnConflictListener() Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)