-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEfDbInvoker.cs
More file actions
53 lines (48 loc) · 2.4 KB
/
Copy pathEfDbInvoker.cs
File metadata and controls
53 lines (48 loc) · 2.4 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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Database;
using CoreEx.Invokers;
using CoreEx.Results;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;
using System.Reflection;
namespace CoreEx.EntityFrameworkCore
{
/// <summary>
/// Provides the standard <see cref="IEfDb"/> invoker functionality.
/// </summary>
/// <remarks>Catches any unhandled <see cref="DbUpdateConcurrencyException"/> or <see cref="DbException"/> and invokes <see cref="Database{TConnection}.HandleDbException(DbException)"/> to handle before bubbling up.</remarks>
public class EfDbInvoker : InvokerBase<IEfDb>
{
/// <inheritdoc/>
protected override TResult OnInvoke<TResult>(InvokeArgs invokeArgs, IEfDb efDb, Func<InvokeArgs, TResult> func) => throw new NotSupportedException();
/// <inheritdoc/>
protected async override Task<TResult> OnInvokeAsync<TResult>(InvokeArgs invokeArgs, IEfDb efdb, Func<InvokeArgs, CancellationToken, Task<TResult>> func, CancellationToken cancellationToken)
{
try
{
return await base.OnInvokeAsync(invokeArgs, efdb, func, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
Result? eresult = null;
if (ex is DbException dbex)
eresult = efdb.Database.HandleDbException(dbex);
else if (ex is DbUpdateConcurrencyException)
eresult = Result.Fail(new ConcurrencyException());
else if (ex is DbUpdateException deux && deux.InnerException != null && deux.InnerException is DbException dbex2)
eresult = efdb.Database.HandleDbException(dbex2);
else if (ex is TargetInvocationException tiex && tiex.InnerException is DbException dbex3)
eresult = efdb.Database.HandleDbException(dbex3);
if (eresult.HasValue && eresult.Value.IsFailure && eresult.Value.Error is CoreEx.Abstractions.IExtendedException)
{
var dresult = default(TResult);
if (dresult is IResult dir)
return (TResult)dir.ToFailure(eresult.Value.Error);
else
eresult.Value.ThrowOnError();
}
throw;
}
}
}
}