forked from CommunityToolkit/Datasync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConflictResolvers.cs
55 lines (49 loc) · 2.31 KB
/
ConflictResolvers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace CommunityToolkit.Datasync.Client.Offline;
/// <summary>
/// An abstract class that provides a mechanism for resolving conflicts between client and server objects of a specified
/// type asynchronously. The object edition of the conflict resolver just calls the typed version.
/// </summary>
/// <typeparam name="TEntity">The type of entity being resolved.</typeparam>
public abstract class AbstractConflictResolver<TEntity> : IConflictResolver<TEntity>
{
/// <inheritdoc />
public abstract Task<ConflictResolution> ResolveConflictAsync(TEntity? clientObject, TEntity? serverObject, CancellationToken cancellationToken = default);
/// <summary>
/// The object version of the resolver calls the typed version.
/// </summary>
/// <param name="clientObject"></param>
/// <param name="serverObject"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<ConflictResolution> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default)
=> await ResolveConflictAsync((TEntity?)clientObject, (TEntity?)serverObject, cancellationToken);
}
/// <summary>
/// A conflict resolver where the client object always wins.
/// </summary>
public class ClientWinsConflictResolver : IConflictResolver
{
/// <inheritdoc />
public Task<ConflictResolution> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default)
=> Task.FromResult(new ConflictResolution
{
Result = ConflictResolutionResult.Client,
Entity = clientObject
});
}
/// <summary>
/// A conflict resolver where the server object always wins.
/// </summary>
public class ServerWinsConflictResolver : IConflictResolver
{
/// <inheritdoc />
public Task<ConflictResolution> ResolveConflictAsync(object? clientObject, object? serverObject, CancellationToken cancellationToken = default)
=> Task.FromResult(new ConflictResolution
{
Result = ConflictResolutionResult.Server,
Entity = serverObject
});
}