Skip to content

Commit d8e0f59

Browse files
committed
Match namespace-class style of codebase
1 parent 78917ae commit d8e0f59

File tree

3 files changed

+145
-140
lines changed

3 files changed

+145
-140
lines changed

Backend.Tests/Mocks/MongoDbContextMock.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@
33
using BackendFramework.Interfaces;
44
using MongoDB.Driver;
55

6-
namespace Backend.Tests.Mocks;
7-
8-
public class MongoDbContextMock : IMongoDbContext
6+
namespace Backend.Tests.Mocks
97
{
10-
public IMongoDatabase Db => throw new NotSupportedException();
8+
public class MongoDbContextMock : IMongoDbContext
9+
{
10+
public IMongoDatabase Db => throw new NotSupportedException();
1111

12-
public Task<IMongoTransaction> BeginTransaction()
13-
=> Task.FromResult<IMongoTransaction>(new MongoTransactionMock());
12+
public Task<IMongoTransaction> BeginTransaction()
13+
=> Task.FromResult<IMongoTransaction>(new MongoTransactionMock());
1414

15-
public Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation)
16-
{
17-
throw new NotImplementedException();
18-
}
15+
public Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation)
16+
{
17+
throw new NotImplementedException();
18+
}
1919

20-
public Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation)
21-
{
22-
throw new NotImplementedException();
23-
}
20+
public Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation)
21+
{
22+
throw new NotImplementedException();
23+
}
2424

25-
private sealed class MongoTransactionMock : IMongoTransaction
26-
{
27-
public IClientSessionHandle Session => null!;
25+
private sealed class MongoTransactionMock : IMongoTransaction
26+
{
27+
public IClientSessionHandle Session => null!;
2828

29-
public Task CommitTransactionAsync() => Task.CompletedTask;
29+
public Task CommitTransactionAsync() => Task.CompletedTask;
3030

31-
public Task AbortTransactionAsync() => Task.CompletedTask;
31+
public Task AbortTransactionAsync() => Task.CompletedTask;
3232

33-
public void Dispose() { }
33+
public void Dispose() { }
34+
}
3435
}
3536
}

Backend/Contexts/MongoDbContext.cs

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,116 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Threading.Tasks;
34
using BackendFramework.Interfaces;
45
using Microsoft.Extensions.Options;
56
using MongoDB.Driver;
67

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
139
{
1410
/// <summary>
15-
/// Gets the configured MongoDB database instance.
11+
/// MongoDB context for accessing the configured database and executing transactional operations.
1612
/// </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
2415
{
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; }
2820

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)
4226
{
43-
session.Dispose();
44-
throw;
27+
var client = new MongoClient(options.Value.ConnectionString);
28+
Db = client.GetDatabase(options.Value.CombineDatabase);
4529
}
46-
}
4730

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()
5836
{
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+
}
6748
}
68-
}
6949

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)
8357
{
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
8666
{
8767
await transaction.AbortTransactionAsync();
88-
return default;
68+
throw;
8969
}
90-
91-
await transaction.CommitTransactionAsync();
92-
return result;
9370
}
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)
9582
{
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+
}
98101
}
99-
}
100102

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;
104106

105-
public IClientSessionHandle Session => _session;
107+
public IClientSessionHandle Session => _session;
106108

107-
public Task CommitTransactionAsync() => _session.CommitTransactionAsync();
109+
public Task CommitTransactionAsync() => _session.CommitTransactionAsync();
108110

109-
public Task AbortTransactionAsync() => _session.AbortTransactionAsync();
111+
public Task AbortTransactionAsync() => _session.AbortTransactionAsync();
110112

111-
public void Dispose() => _session.Dispose();
113+
public void Dispose() => _session.Dispose();
114+
}
112115
}
113116
}

Backend/Interfaces/IMongoDbContext.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,51 @@
22
using System.Threading.Tasks;
33
using MongoDB.Driver;
44

5-
namespace BackendFramework.Interfaces;
6-
7-
/// <summary>
8-
/// Abstraction over MongoDB database access and transaction execution.
9-
/// </summary>
10-
public interface IMongoDbContext
5+
namespace BackendFramework.Interfaces
116
{
127
/// <summary>
13-
/// Gets the configured MongoDB database instance.
8+
/// Abstraction over MongoDB database access and transaction execution.
149
/// </summary>
15-
IMongoDatabase Db { get; }
10+
public interface IMongoDbContext
11+
{
12+
/// <summary>
13+
/// Gets the configured MongoDB database instance.
14+
/// </summary>
15+
IMongoDatabase Db { get; }
1616

17-
/// <summary>
18-
/// Begins a new transaction and returns the transaction wrapper.
19-
/// </summary>
20-
/// <returns>A transaction wrapper containing the active client session.</returns>
21-
Task<IMongoTransaction> BeginTransaction();
17+
/// <summary>
18+
/// Begins a new transaction and returns the transaction wrapper.
19+
/// </summary>
20+
/// <returns>A transaction wrapper containing the active client session.</returns>
21+
Task<IMongoTransaction> BeginTransaction();
2222

23-
/// <summary>
24-
/// Executes an operation in a transaction, committing on success and aborting on exception.
25-
/// </summary>
26-
/// <typeparam name="T">The operation result type.</typeparam>
27-
/// <param name="operation">Operation to execute with the transaction session.</param>
28-
/// <returns>The operation result.</returns>
29-
Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation);
23+
/// <summary>
24+
/// Executes an operation in a transaction, committing on success and aborting on exception.
25+
/// </summary>
26+
/// <typeparam name="T">The operation result type.</typeparam>
27+
/// <param name="operation">Operation to execute with the transaction session.</param>
28+
/// <returns>The operation result.</returns>
29+
Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation);
30+
31+
/// <summary>
32+
/// Executes an operation in a transaction, committing only when a non-null result is returned.
33+
/// Null represents an operation that could complete and shouldn't be committed, so it aborts.
34+
/// </summary>
35+
/// <typeparam name="T">The operation result type.</typeparam>
36+
/// <param name="operation">Operation to execute with the transaction session.</param>
37+
/// <returns>
38+
/// The operation result when non-null; otherwise <see langword="null"/> after aborting the transaction.
39+
/// </returns>
40+
Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation);
41+
}
3042

3143
/// <summary>
32-
/// Executes an operation in a transaction, committing only when a non-null result is returned.
33-
/// Null represents an operation that could complete and shouldn't be committed, so it aborts.
44+
/// Represents a MongoDB transaction wrapper that exposes the active session and transaction controls.
3445
/// </summary>
35-
/// <typeparam name="T">The operation result type.</typeparam>
36-
/// <param name="operation">Operation to execute with the transaction session.</param>
37-
/// <returns>
38-
/// The operation result when non-null; otherwise <see langword="null"/> after aborting the transaction.
39-
/// </returns>
40-
Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation);
41-
}
42-
43-
/// <summary>
44-
/// Represents a MongoDB transaction wrapper that exposes the active session and transaction controls.
45-
/// </summary>
46-
public interface IMongoTransaction : IDisposable
47-
{
48-
IClientSessionHandle Session { get; }
49-
Task CommitTransactionAsync();
50-
Task AbortTransactionAsync();
46+
public interface IMongoTransaction : IDisposable
47+
{
48+
IClientSessionHandle Session { get; }
49+
Task CommitTransactionAsync();
50+
Task AbortTransactionAsync();
51+
}
5152
}

0 commit comments

Comments
 (0)