From f13c6ef9673cefcd4aae34922dcf3bf3c16f12bb Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 21 Aug 2018 12:00:21 +0800 Subject: [PATCH] Add cap dbtransaction wapper --- .../IClientSessionHandle.CAP.cs | 76 +++++++++++++++++++ .../IDbContextTransaction.CAP.cs | 35 +++++++++ .../IDbContextTransaction.CAP.cs | 35 +++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/DotNetCore.CAP.MongoDB/IClientSessionHandle.CAP.cs create mode 100644 src/DotNetCore.CAP.PostgreSql/IDbContextTransaction.CAP.cs create mode 100644 src/DotNetCore.CAP.SqlServer/IDbContextTransaction.CAP.cs diff --git a/src/DotNetCore.CAP.MongoDB/IClientSessionHandle.CAP.cs b/src/DotNetCore.CAP.MongoDB/IClientSessionHandle.CAP.cs new file mode 100644 index 0000000..3381d02 --- /dev/null +++ b/src/DotNetCore.CAP.MongoDB/IClientSessionHandle.CAP.cs @@ -0,0 +1,76 @@ +using System.Threading; +using System.Threading.Tasks; +using DotNetCore.CAP; +using MongoDB.Bson; +using MongoDB.Driver.Core.Bindings; + +// ReSharper disable once CheckNamespace +namespace MongoDB.Driver +{ + internal class CapMongoDbClientSessionHandle : IClientSessionHandle + { + private readonly ICapTransaction _transaction; + private readonly IClientSessionHandle _sessionHandle; + + public CapMongoDbClientSessionHandle(ICapTransaction transaction) + { + _transaction = transaction; + _sessionHandle = (IClientSessionHandle)_transaction.DbTransaction; + } + + public void Dispose() + { + _transaction.Dispose(); + } + + public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken)) + { + _transaction.Rollback(); + } + + public Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + _transaction.Rollback(); + return Task.CompletedTask; + } + + public void AdvanceClusterTime(BsonDocument newClusterTime) + { + _sessionHandle.AdvanceClusterTime(newClusterTime); + } + + public void AdvanceOperationTime(BsonTimestamp newOperationTime) + { + _sessionHandle.AdvanceOperationTime(newOperationTime); + } + + public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken)) + { + _transaction.Commit(); + } + + public Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + _transaction.Commit(); + return Task.CompletedTask; + } + + public void StartTransaction(TransactionOptions transactionOptions = null) + { + _sessionHandle.StartTransaction(transactionOptions); + } + + public IMongoClient Client => _sessionHandle.Client; + public BsonDocument ClusterTime => _sessionHandle.ClusterTime; + public bool IsImplicit => _sessionHandle.IsImplicit; + public bool IsInTransaction => _sessionHandle.IsInTransaction; + public BsonTimestamp OperationTime => _sessionHandle.OperationTime; + public ClientSessionOptions Options => _sessionHandle.Options; + public IServerSession ServerSession => _sessionHandle.ServerSession; + public ICoreSessionHandle WrappedCoreSession => _sessionHandle.WrappedCoreSession; + public IClientSessionHandle Fork() + { + return _sessionHandle.Fork(); + } + } +} diff --git a/src/DotNetCore.CAP.PostgreSql/IDbContextTransaction.CAP.cs b/src/DotNetCore.CAP.PostgreSql/IDbContextTransaction.CAP.cs new file mode 100644 index 0000000..dbfa691 --- /dev/null +++ b/src/DotNetCore.CAP.PostgreSql/IDbContextTransaction.CAP.cs @@ -0,0 +1,35 @@ +using System; +using DotNetCore.CAP; + +// ReSharper disable once CheckNamespace +namespace Microsoft.EntityFrameworkCore.Storage +{ + internal class CapEFDbTransaction : IDbContextTransaction + { + private readonly ICapTransaction _transaction; + + public CapEFDbTransaction(ICapTransaction transaction) + { + _transaction = transaction; + var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction; + TransactionId = dbContextTransaction.TransactionId; + } + + public void Dispose() + { + _transaction.Dispose(); + } + + public void Commit() + { + _transaction.Commit(); + } + + public void Rollback() + { + _transaction.Rollback(); + } + + public Guid TransactionId { get; } + } +} diff --git a/src/DotNetCore.CAP.SqlServer/IDbContextTransaction.CAP.cs b/src/DotNetCore.CAP.SqlServer/IDbContextTransaction.CAP.cs new file mode 100644 index 0000000..dbfa691 --- /dev/null +++ b/src/DotNetCore.CAP.SqlServer/IDbContextTransaction.CAP.cs @@ -0,0 +1,35 @@ +using System; +using DotNetCore.CAP; + +// ReSharper disable once CheckNamespace +namespace Microsoft.EntityFrameworkCore.Storage +{ + internal class CapEFDbTransaction : IDbContextTransaction + { + private readonly ICapTransaction _transaction; + + public CapEFDbTransaction(ICapTransaction transaction) + { + _transaction = transaction; + var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction; + TransactionId = dbContextTransaction.TransactionId; + } + + public void Dispose() + { + _transaction.Dispose(); + } + + public void Commit() + { + _transaction.Commit(); + } + + public void Rollback() + { + _transaction.Rollback(); + } + + public Guid TransactionId { get; } + } +}