From 2ec580648ac62a3e890ba36ad5c92e879d93d027 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 20 Aug 2018 18:54:40 +0800 Subject: [PATCH] Add RelationDbTransaction and extensions --- .../Internal/RelationDbTransaction.cs | 38 ++++++++++++++++++ src/DotNetCore.CAP/TransactionExtensions.cs | 40 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/DotNetCore.CAP/Internal/RelationDbTransaction.cs create mode 100644 src/DotNetCore.CAP/TransactionExtensions.cs diff --git a/src/DotNetCore.CAP/Internal/RelationDbTransaction.cs b/src/DotNetCore.CAP/Internal/RelationDbTransaction.cs new file mode 100644 index 0000000..10cee48 --- /dev/null +++ b/src/DotNetCore.CAP/Internal/RelationDbTransaction.cs @@ -0,0 +1,38 @@ +// Copyright (c) .NET Core Community. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Data; + +namespace DotNetCore.CAP.Internal +{ + internal class RelationDbTransaction : IDbTransaction + { + private readonly ICapTransaction _capTransaction; + + public RelationDbTransaction(ICapTransaction capTransaction) + { + _capTransaction = capTransaction; + var dbTransaction = (IDbTransaction) capTransaction.DbTransaction; + Connection = dbTransaction.Connection; + IsolationLevel = dbTransaction.IsolationLevel; + } + + public void Dispose() + { + _capTransaction.Dispose(); + } + + public void Commit() + { + _capTransaction.Commit(); + } + + public void Rollback() + { + _capTransaction.Rollback(); + } + + public IDbConnection Connection { get; } + public IsolationLevel IsolationLevel { get; } + } +} \ No newline at end of file diff --git a/src/DotNetCore.CAP/TransactionExtensions.cs b/src/DotNetCore.CAP/TransactionExtensions.cs new file mode 100644 index 0000000..e8af0e4 --- /dev/null +++ b/src/DotNetCore.CAP/TransactionExtensions.cs @@ -0,0 +1,40 @@ +// Copyright (c) .NET Core Community. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Data; +using DotNetCore.CAP.Internal; + +namespace DotNetCore.CAP +{ + public static class TransactionExtensions + { + public static ICapTransaction Begin(this ICapTransaction transaction, + IDbTransaction dbTransaction, bool autoCommit = false) + { + + transaction.DbTransaction = dbTransaction; + transaction.AutoCommit = autoCommit; + + return transaction; + } + + public static IDbTransaction JoinToTransaction(this IDbTransaction dbTransaction, + ICapPublisher publisher, bool autoCommit = false) + { + dbTransaction = new RelationDbTransaction(publisher.Transaction.Begin(dbTransaction, autoCommit)); + return dbTransaction; + } + + public static IDbTransaction BeginAndJoinToTransaction(this IDbConnection dbConnection, + ICapPublisher publisher, bool autoCommit = false) + { + if (dbConnection.State == ConnectionState.Closed) + { + dbConnection.Open(); + } + + var dbTransaction = dbConnection.BeginTransaction(); + return dbTransaction.JoinToTransaction(publisher, autoCommit); + } + } +} \ No newline at end of file