|
1 | 1 | using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
2 | 3 | using System.Threading.Tasks; |
3 | 4 | using BackendFramework.Interfaces; |
4 | 5 | using Microsoft.Extensions.Options; |
5 | 6 | using MongoDB.Driver; |
6 | 7 |
|
7 | | -namespace BackendFramework.Contexts; |
8 | | - |
9 | | -/// <summary> |
10 | | -/// MongoDB context for accessing the configured database and executing transactional operations. |
11 | | -/// </summary> |
12 | | -public class MongoDbContext : IMongoDbContext |
| 8 | +namespace BackendFramework.Contexts |
13 | 9 | { |
14 | 10 | /// <summary> |
15 | | - /// Gets the configured MongoDB database instance. |
| 11 | + /// MongoDB context for accessing the configured database and executing transactional operations. |
16 | 12 | /// </summary> |
17 | | - public IMongoDatabase Db { get; } |
18 | | - |
19 | | - /// <summary> |
20 | | - /// Creates a new <see cref="MongoDbContext"/> from application settings. |
21 | | - /// </summary> |
22 | | - /// <param name="options">Options containing the Mongo connection string and database name.</param> |
23 | | - public MongoDbContext(IOptions<Startup.Settings> options) |
| 13 | + [ExcludeFromCodeCoverage] |
| 14 | + public class MongoDbContext : IMongoDbContext |
24 | 15 | { |
25 | | - var client = new MongoClient(options.Value.ConnectionString); |
26 | | - Db = client.GetDatabase(options.Value.CombineDatabase); |
27 | | - } |
| 16 | + /// <summary> |
| 17 | + /// Gets the configured MongoDB database instance. |
| 18 | + /// </summary> |
| 19 | + public IMongoDatabase Db { get; } |
28 | 20 |
|
29 | | - /// <summary> |
30 | | - /// Begins a MongoDB transaction and returns a disposable transaction wrapper. |
31 | | - /// </summary> |
32 | | - /// <returns>A transaction wrapper containing the active client session.</returns> |
33 | | - public async Task<IMongoTransaction> BeginTransaction() |
34 | | - { |
35 | | - var session = await Db.Client.StartSessionAsync(); |
36 | | - try |
37 | | - { |
38 | | - session.StartTransaction(); |
39 | | - return new MongoTransactionWrapper(session); |
40 | | - } |
41 | | - catch |
| 21 | + /// <summary> |
| 22 | + /// Creates a new <see cref="MongoDbContext"/> from application settings. |
| 23 | + /// </summary> |
| 24 | + /// <param name="options">Options containing the Mongo connection string and database name.</param> |
| 25 | + public MongoDbContext(IOptions<Startup.Settings> options) |
42 | 26 | { |
43 | | - session.Dispose(); |
44 | | - throw; |
| 27 | + var client = new MongoClient(options.Value.ConnectionString); |
| 28 | + Db = client.GetDatabase(options.Value.CombineDatabase); |
45 | 29 | } |
46 | | - } |
47 | 30 |
|
48 | | - /// <summary> |
49 | | - /// Executes an operation in a transaction, committing on success and aborting on exception. |
50 | | - /// </summary> |
51 | | - /// <typeparam name="T">The operation result type.</typeparam> |
52 | | - /// <param name="operation">Operation to execute with the transaction session.</param> |
53 | | - /// <returns>The operation result.</returns> |
54 | | - public async Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation) |
55 | | - { |
56 | | - using var transaction = await BeginTransaction(); |
57 | | - try |
| 31 | + /// <summary> |
| 32 | + /// Begins a MongoDB transaction and returns a disposable transaction wrapper. |
| 33 | + /// </summary> |
| 34 | + /// <returns>A transaction wrapper containing the active client session.</returns> |
| 35 | + public async Task<IMongoTransaction> BeginTransaction() |
58 | 36 | { |
59 | | - var result = await operation(transaction.Session); |
60 | | - await transaction.CommitTransactionAsync(); |
61 | | - return result; |
62 | | - } |
63 | | - catch |
64 | | - { |
65 | | - await transaction.AbortTransactionAsync(); |
66 | | - throw; |
| 37 | + var session = await Db.Client.StartSessionAsync(); |
| 38 | + try |
| 39 | + { |
| 40 | + session.StartTransaction(); |
| 41 | + return new MongoTransactionWrapper(session); |
| 42 | + } |
| 43 | + catch |
| 44 | + { |
| 45 | + session.Dispose(); |
| 46 | + throw; |
| 47 | + } |
67 | 48 | } |
68 | | - } |
69 | 49 |
|
70 | | - /// <summary> |
71 | | - /// Executes an operation in a transaction, committing when a non-null result is returned. |
72 | | - /// Null represents an operation that could complete and shouldn't be committed, so it aborts. |
73 | | - /// </summary> |
74 | | - /// <typeparam name="T">The operation result type.</typeparam> |
75 | | - /// <param name="operation">Operation to execute with the transaction session.</param> |
76 | | - /// <returns> |
77 | | - /// The operation result when non-null; otherwise <see langword="null"/> after aborting the transaction. |
78 | | - /// </returns> |
79 | | - public async Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation) |
80 | | - { |
81 | | - using var transaction = await BeginTransaction(); |
82 | | - try |
| 50 | + /// <summary> |
| 51 | + /// Executes an operation in a transaction, committing on success and aborting on exception. |
| 52 | + /// </summary> |
| 53 | + /// <typeparam name="T">The operation result type.</typeparam> |
| 54 | + /// <param name="operation">Operation to execute with the transaction session.</param> |
| 55 | + /// <returns>The operation result.</returns> |
| 56 | + public async Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation) |
83 | 57 | { |
84 | | - var result = await operation(transaction.Session); |
85 | | - if (result is null) |
| 58 | + using var transaction = await BeginTransaction(); |
| 59 | + try |
| 60 | + { |
| 61 | + var result = await operation(transaction.Session); |
| 62 | + await transaction.CommitTransactionAsync(); |
| 63 | + return result; |
| 64 | + } |
| 65 | + catch |
86 | 66 | { |
87 | 67 | await transaction.AbortTransactionAsync(); |
88 | | - return default; |
| 68 | + throw; |
89 | 69 | } |
90 | | - |
91 | | - await transaction.CommitTransactionAsync(); |
92 | | - return result; |
93 | 70 | } |
94 | | - catch |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Executes an operation in a transaction, committing when a non-null result is returned. |
| 74 | + /// Null represents an operation that could complete and shouldn't be committed, so it aborts. |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="T">The operation result type.</typeparam> |
| 77 | + /// <param name="operation">Operation to execute with the transaction session.</param> |
| 78 | + /// <returns> |
| 79 | + /// The operation result when non-null; otherwise <see langword="null"/> after aborting the transaction. |
| 80 | + /// </returns> |
| 81 | + public async Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation) |
95 | 82 | { |
96 | | - await transaction.AbortTransactionAsync(); |
97 | | - throw; |
| 83 | + using var transaction = await BeginTransaction(); |
| 84 | + try |
| 85 | + { |
| 86 | + var result = await operation(transaction.Session); |
| 87 | + if (result is null) |
| 88 | + { |
| 89 | + await transaction.AbortTransactionAsync(); |
| 90 | + return default; |
| 91 | + } |
| 92 | + |
| 93 | + await transaction.CommitTransactionAsync(); |
| 94 | + return result; |
| 95 | + } |
| 96 | + catch |
| 97 | + { |
| 98 | + await transaction.AbortTransactionAsync(); |
| 99 | + throw; |
| 100 | + } |
98 | 101 | } |
99 | | - } |
100 | 102 |
|
101 | | - private class MongoTransactionWrapper(IClientSessionHandle session) : IMongoTransaction |
102 | | - { |
103 | | - private readonly IClientSessionHandle _session = session; |
| 103 | + private class MongoTransactionWrapper(IClientSessionHandle session) : IMongoTransaction |
| 104 | + { |
| 105 | + private readonly IClientSessionHandle _session = session; |
104 | 106 |
|
105 | | - public IClientSessionHandle Session => _session; |
| 107 | + public IClientSessionHandle Session => _session; |
106 | 108 |
|
107 | | - public Task CommitTransactionAsync() => _session.CommitTransactionAsync(); |
| 109 | + public Task CommitTransactionAsync() => _session.CommitTransactionAsync(); |
108 | 110 |
|
109 | | - public Task AbortTransactionAsync() => _session.AbortTransactionAsync(); |
| 111 | + public Task AbortTransactionAsync() => _session.AbortTransactionAsync(); |
110 | 112 |
|
111 | | - public void Dispose() => _session.Dispose(); |
| 113 | + public void Dispose() => _session.Dispose(); |
| 114 | + } |
112 | 115 | } |
113 | 116 | } |
0 commit comments