Listeners: What are conflict listeners and how to work with them?

To allow users to handle document replication conflicts automatically, we introduced a Document Conflict listener. To create your own listener of this type, just implement IDocumentConflictListener interface.

public interface IDocumentConflictListener
{
	bool TryResolveConflict(string key, JsonDocument[] conflictedDocs, 
							out JsonDocument resolvedDocument);
}

Example

This example shows how to create an automatic conflict resolver by using IDocumentConflictListener, which will pick the newest item from the list of conflicted documents:

public class ResolveInFavourOfNewest : IDocumentConflictListener
{
	public bool TryResolveConflict(
		string key,
		JsonDocument[] conflictedDocs,
		out JsonDocument resolvedDocument)
	{
		DateTime? maxDate = conflictedDocs.Max(x => x.LastModified);
		resolvedDocument = conflictedDocs
							.FirstOrDefault(x => x.LastModified == maxDate);

		if (resolvedDocument == null)
			return false;

		resolvedDocument.Metadata.Remove("@id");
		resolvedDocument.Metadata.Remove("@etag");
		return true;
	}
}