Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ namespace Paramore.Brighter.MongoDb;
/// This interface combines MongoDB client access with transaction management
/// using <see cref="IClientSession"/> for ACID guarantees.
/// </summary>
public interface IAmAMongoDbTransactionProvider : IAmAMongoDbConnectionProvider, IAmABoxTransactionProvider<IClientSession>;
public interface IAmAMongoDbTransactionProvider : IAmAMongoDbConnectionProvider,
IAmABoxTransactionProvider<IClientSession>;
20 changes: 15 additions & 5 deletions src/Paramore.Brighter.MongoDb/MongoDbUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Paramore.Brighter.MongoDb;
/// to offer both connection and transaction management.
/// </remarks>
/// <param name="configuration">The MongoDB configuration used to obtain the client.</param>
public class MongoDbUnitOfWork(IAmAMongoDbConfiguration configuration) : IAmAMongoDbTransactionProvider
public class MongoDbUnitOfWork(IAmAMongoDbConfiguration configuration) : IAmAMongoDbTransactionProvider, IAmABoxTransactionProvider<IClientSessionHandle>
{
private IClientSession? _session;
private IClientSessionHandle? _session;

/// <inheritdoc />
public IMongoClient Client { get; } = configuration.Client;
Expand Down Expand Up @@ -102,14 +102,24 @@ public async Task RollbackAsync(CancellationToken cancellationToken = default)
}

/// <inheritdoc />
public IClientSession GetTransaction()
public IClientSessionHandle GetTransaction()
{
return _session = Client.StartSession();
}

/// <inheritdoc />
public async Task<IClientSession> GetTransactionAsync(CancellationToken cancellationToken = default)
public async Task<IClientSessionHandle> GetTransactionAsync(CancellationToken cancellationToken = default)
{
return _session = await Client.StartSessionAsync(cancellationToken: cancellationToken);
}

async Task<IClientSession> IAmABoxTransactionProvider<IClientSession>.GetTransactionAsync(CancellationToken cancellationToken)
{
return await GetTransactionAsync(cancellationToken);
}

IClientSession IAmABoxTransactionProvider<IClientSession>.GetTransaction()
{
return GetTransaction();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using MongoDB.Driver;
using Xunit;

namespace Paramore.Brighter.MongoDb.Tests.Outbox;

[Trait("Category", "MongoDb")]
public class MongoDbValidateTransactionProvider
{
[Fact]
public void When_Resolving_Transaction_Provider()
{
Type transactionProviderInterface = typeof(IAmABoxTransactionProvider<>);
Type? transactionType = null;
foreach (Type i in typeof(MongoDbUnitOfWork).GetInterfaces())
{
if (i.IsGenericType && i.GetGenericTypeDefinition() == transactionProviderInterface)
{
transactionType = i.GetGenericArguments()[0];
}
}

Assert.NotNull(transactionType);
Assert.Equal(typeof(IClientSessionHandle) , transactionType);
}
}
Loading