-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEfDbModel.Delete.cs
More file actions
86 lines (73 loc) · 4.97 KB
/
Copy pathEfDbModel.Delete.cs
File metadata and controls
86 lines (73 loc) · 4.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace CoreEx.EntityFrameworkCore;
public partial class EfDbModel<TModel>
{
/// <summary>
/// Deletes the model for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="DataResult"/></returns>
/// <remarks>A delete is considered idempotent and as such no <see cref="NotFoundException"/> will be thrown. The returning <see cref="DataResult.WasMutated"/> is informational only.</remarks>
public Task<DataResult> DeleteAsync(CompositeKey key, CancellationToken cancellationToken = default) => DeleteAsync(Args, key, cancellationToken);
/// <summary>
/// Deletes the model for the specified <paramref name="key"/>.
/// </summary>
/// <param name="args">The <see cref="EfDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="DataResult"/></returns>
/// <remarks>A delete is considered idempotent and as such no <see cref="NotFoundException"/> will be thrown. The returning <see cref="DataResult.WasMutated"/> is informational only.</remarks>
public async Task<DataResult> DeleteAsync(EfDbArgs args, CompositeKey key, CancellationToken cancellationToken = default) => (await DeleteWithResultInternalAsync(args, key, nameof(DeleteAsync), cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the model for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="DataResult"/></returns>
/// <remarks>A delete is considered idempotent and as such no <see cref="NotFoundException"/> will be thrown. The returning <see cref="DataResult.WasMutated"/> is informational only.</remarks>
public Task<Result<DataResult>> DeleteWithResultAsync(CompositeKey key, CancellationToken cancellationToken = default) => DeleteWithResultAsync(Args, key, cancellationToken);
/// <summary>
/// Deletes the model for the specified <paramref name="key"/>.
/// </summary>
/// <param name="args">The <see cref="EfDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="DataResult"/></returns>
/// <remarks>A delete is considered idempotent and as such no <see cref="NotFoundException"/> will be thrown. The returning <see cref="DataResult.WasMutated"/> is informational only.</remarks>
public Task<Result<DataResult>> DeleteWithResultAsync(EfDbArgs args, CompositeKey key, CancellationToken cancellationToken = default) => DeleteWithResultInternalAsync(args, key, nameof(DeleteWithResultAsync), cancellationToken);
/// <summary>
/// Deletes the model (internal).
/// </summary>
private async Task<Result<DataResult>> DeleteWithResultInternalAsync(EfDbArgs args, CompositeKey key, string memberName, CancellationToken cancellationToken = default) => await EfDb.Invoker.InvokeAsync(EfDb, args, async (_, args, cancellationToken) =>
{
// Check (find) to determine if the model exists.
var model = await EfDb.DbContext.FindAsync<TModel>([.. key.Args], cancellationToken).ConfigureAwait(false);
var cmr = CheckModel(args, model, OperationType.Delete, treatNullAsNotFound: true);
// Where not found, return false (a delete is considered idempotent).
if (cmr.IsNotFoundError)
return Result.Ok(DataResult.False);
if (cmr.IsFailure)
return cmr.Bind();
// Delete or logical delete as appropriate.
switch (Options.LogicalDeleteSupport)
{
// Physical delete.
case FeatureSupport.NotSupported:
EfDb.DbContext.Remove(model!);
break;
// Logical delete (ambiguous exception).
case FeatureSupport.ReadOnly:
throw new InvalidOperationException($"The '{nameof(Options)}.{nameof(Options.LogicalDeleteSupport)}' is set to '{nameof(FeatureSupport.ReadOnly)}' which is ambiguous for a delete operation; the model must implement '{nameof(ILogicallyDeleted)}' not '{nameof(IReadOnlyLogicallyDeleted)}'.");
// Logical delete (update).
case FeatureSupport.Mutable:
var ld = (ILogicallyDeleted)model!;
ld.IsDeleted = true;
Model.PrepareUpdate(model, EfDb.ExecutionContext);
EfDb.DbContext.Update(model!);
break;
}
if (args.SaveChanges)
await EfDb.DbContext.SaveChangesAsync(true, cancellationToken).ConfigureAwait(false);
return Result.Ok(DataResult.True);
}, cancellationToken, memberName).ConfigureAwait(false);
}