From 5be6739cde9fa960360b2832e5af7c2560473cf5 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 21 Aug 2018 12:01:02 +0800 Subject: [PATCH] add and refactor transaction extension --- .../ICapTransaction.MongoDB.cs | 23 +++++++++++++++---- .../ICapTransaction.MySql.cs | 3 +-- .../ICapTransaction.PostgreSql.cs | 15 ++++++------ .../ICapTransaction.SqlServer.cs | 16 ++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs b/src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs index fc7b1a6..4b85f1d 100644 --- a/src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs +++ b/src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs @@ -1,5 +1,4 @@ -using System.Data; -using System.Diagnostics; +using System.Diagnostics; using MongoDB.Driver; // ReSharper disable once CheckNamespace @@ -36,7 +35,7 @@ namespace DotNetCore.CAP public override void Dispose() { - (DbTransaction as IDbTransaction)?.Dispose(); + (DbTransaction as IClientSessionHandle)?.Dispose(); } } @@ -49,11 +48,25 @@ namespace DotNetCore.CAP { dbTransaction.StartTransaction(); } - + transaction.DbTransaction = dbTransaction; transaction.AutoCommit = autoCommit; return transaction; - } + } + + public static IClientSessionHandle BeginAndJoinToTransaction(this IClientSessionHandle clientSessionHandle, + ICapPublisher publisher, bool autoCommit = false) + { + var capTrans = publisher.Transaction.Begin(clientSessionHandle, autoCommit); + return new CapMongoDbClientSessionHandle(capTrans); + } + + public static IClientSessionHandle StartAndJoinToTransaction(this IMongoClient client, + ICapPublisher publisher, bool autoCommit = false) + { + var clientSessionHandle = client.StartSession(); + return BeginAndJoinToTransaction(clientSessionHandle, publisher, autoCommit); + } } } diff --git a/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs b/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs index 7a79f91..2dce16c 100644 --- a/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs +++ b/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs @@ -55,7 +55,6 @@ namespace DotNetCore.CAP public static ICapTransaction Begin(this ICapTransaction transaction, IDbContextTransaction dbTransaction, bool autoCommit = false) { - transaction.DbTransaction = dbTransaction; transaction.AutoCommit = autoCommit; @@ -66,7 +65,7 @@ namespace DotNetCore.CAP ICapPublisher publisher, bool autoCommit = false) { var trans = database.BeginTransaction(); - var capTrans = publisher.Transaction.Begin(trans); + var capTrans = publisher.Transaction.Begin(trans, autoCommit); return new CapEFDbTransaction(capTrans); } } diff --git a/src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs b/src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs index 7599e39..a14914c 100644 --- a/src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs +++ b/src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs @@ -1,5 +1,6 @@ using System.Data; using System.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; // ReSharper disable once CheckNamespace @@ -50,7 +51,7 @@ namespace DotNetCore.CAP public static class CapTransactionExtensions { public static ICapTransaction Begin(this ICapTransaction transaction, - IDbTransaction dbTransaction, bool autoCommit = false) + IDbContextTransaction dbTransaction, bool autoCommit = false) { transaction.DbTransaction = dbTransaction; transaction.AutoCommit = autoCommit; @@ -58,14 +59,12 @@ namespace DotNetCore.CAP return transaction; } - public static ICapTransaction Begin(this ICapTransaction transaction, - IDbContextTransaction dbTransaction, bool autoCommit = false) + public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database, + ICapPublisher publisher, bool autoCommit = false) { - - transaction.DbTransaction = dbTransaction; - transaction.AutoCommit = autoCommit; - - return transaction; + var trans = database.BeginTransaction(); + var capTrans = publisher.Transaction.Begin(trans, autoCommit); + return new CapEFDbTransaction(capTrans); } } diff --git a/src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs index e44c7cb..14e80b0 100644 --- a/src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs @@ -1,5 +1,6 @@ using System.Data; using System.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; // ReSharper disable once CheckNamespace @@ -50,23 +51,20 @@ namespace DotNetCore.CAP public static class CapTransactionExtensions { public static ICapTransaction Begin(this ICapTransaction transaction, - IDbTransaction dbTransaction, bool autoCommit = false) + IDbContextTransaction dbTransaction, bool autoCommit = false) { - transaction.DbTransaction = dbTransaction; transaction.AutoCommit = autoCommit; return transaction; } - public static ICapTransaction Begin(this ICapTransaction transaction, - IDbContextTransaction dbTransaction, bool autoCommit = false) + public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database, + ICapPublisher publisher, bool autoCommit = false) { - - transaction.DbTransaction = dbTransaction; - transaction.AutoCommit = autoCommit; - - return transaction; + var trans = database.BeginTransaction(); + var capTrans = publisher.Transaction.Begin(trans, autoCommit); + return new CapEFDbTransaction(capTrans); } }