|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +namespace CommunityToolkit.Datasync.Client.Offline; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// An abstract class that provides a mechanism for resolving conflicts between client and server objects of a specified |
| 9 | +/// type asynchronously. The object edition of the conflict resolver just calls the typed version. |
| 10 | +/// </summary> |
| 11 | +/// <typeparam name="TEntity">The type of entity being resolved.</typeparam> |
| 12 | +public abstract class AbstractConflictResolver<TEntity> : IConflictResolver<TEntity> |
| 13 | +{ |
| 14 | + /// <inheritdoc /> |
| 15 | + public abstract Task<TEntity?> ResolveConflictAsync(TEntity? clientObject, TEntity? serverObject, CancellationToken cancellationToken = default); |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The object version of the resolver calls the typed version. |
| 19 | + /// </summary> |
| 20 | + /// <param name="clientObject"></param> |
| 21 | + /// <param name="serverObject"></param> |
| 22 | + /// <param name="cancellationToken"></param> |
| 23 | + /// <returns></returns> |
| 24 | + public virtual async Task<object?> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default) |
| 25 | + => await ResolveConflictAsync((TEntity?)clientObject, (TEntity?)serverObject, cancellationToken); |
| 26 | +} |
| 27 | + |
| 28 | +/// <summary> |
| 29 | +/// A conflict resolver where the client object always wins. |
| 30 | +/// </summary> |
| 31 | +public class ClientWinsConflictResolver : IConflictResolver |
| 32 | +{ |
| 33 | + /// <inheritdoc /> |
| 34 | + public Task<object?> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default) |
| 35 | + => Task.FromResult(clientObject); |
| 36 | +} |
| 37 | + |
| 38 | +/// <summary> |
| 39 | +/// A conflict resolver where the server object always wins. |
| 40 | +/// </summary> |
| 41 | +public class ServerWinsConflictResolver : IConflictResolver |
| 42 | +{ |
| 43 | + /// <inheritdoc /> |
| 44 | + public Task<object?> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default) |
| 45 | + => Task.FromResult(serverObject); |
| 46 | +} |
| 47 | + |
0 commit comments