Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/Session/ISessionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace MongoDB.Extensions.Session;

public interface ISessionFactory
{
ValueTask<IClientSessionHandle> CreateSessionAsync(
CancellationToken cancellationToken);
}
22 changes: 22 additions & 0 deletions src/Session/Internal/MongoClientSessionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace MongoDB.Extensions.Session;

internal class MongoClientSessionFactory : ISessionFactory
{
private readonly IMongoClient _client;

public MongoClientSessionFactory(IMongoClient client)
{
_client = client;
}

public async ValueTask<IClientSessionHandle> CreateSessionAsync(
CancellationToken cancellationToken)
{
return await _client
.StartSessionAsync(cancellationToken: cancellationToken);
}
}
26 changes: 17 additions & 9 deletions src/Session/Internal/MongoSessionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

namespace MongoDB.Extensions.Session;

public class MongoSessionProvider<TContext, TScope> : ISessionProvider<TScope>
public class MongoSessionProvider<TContext, TScope> : MongoSessionProvider<TScope>
where TContext : IMongoDbContext
{
private readonly IMongoClient _mongoClient;
public MongoSessionProvider(TContext context)
: base(new MongoClientSessionFactory(context.Client))
{
}
}

public MongoSessionProvider(TContext context)
public class MongoSessionProvider<TScope> : ISessionProvider<TScope>
{
private readonly ISessionFactory _sessionFactory;

protected MongoSessionProvider(ISessionFactory sessionFactory)
{
_mongoClient = context.Client;
_sessionFactory = sessionFactory;
}

protected virtual TransactionOptions TransactionOptions { get; } = new(
Expand All @@ -25,8 +33,8 @@ public MongoSessionProvider(TContext context)
public async Task<ITransactionSession> BeginTransactionAsync(
CancellationToken cancellationToken)
{
IClientSessionHandle clientSession = await _mongoClient
.StartSessionAsync(cancellationToken: cancellationToken);
IClientSessionHandle clientSession = await _sessionFactory
.CreateSessionAsync(cancellationToken);

clientSession.StartTransaction(TransactionOptions);

Expand All @@ -36,9 +44,9 @@ public async Task<ITransactionSession> BeginTransactionAsync(
public async Task<ISession> StartSessionAsync(
CancellationToken cancellationToken)
{
IClientSessionHandle clientSession = await _mongoClient
.StartSessionAsync(cancellationToken: cancellationToken);
IClientSessionHandle clientSession = await _sessionFactory
.CreateSessionAsync(cancellationToken);

return new MongoSession(clientSession, TransactionOptions);
}
}
}
Loading