From ab712361e79e41310bb0a38927c82ab91b789937 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 13 May 2022 17:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=AF=B9ISupportShardingTran?= =?UTF-8?q?saction=E7=9A=84=E4=BE=9D=E8=B5=96=E7=9B=B4=E6=8E=A5=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=88=B0IShardingDBContext=E5=B9=B6=E4=B8=94=E5=8F=91?= =?UTF-8?q?=E5=B8=83x.4.3.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget-publish.bat | 8 ++-- .../AbstractShardingAbpDbContext.cs | 2 +- .../ShardingRelationalTransaction.cs | 40 +++++++++---------- .../ShardingRelationalTransactionFactory.cs | 12 +++--- .../ShardingRelationalTransactionManager.cs | 19 ++++----- .../Sharding/AbstractShardingDbContext.cs | 2 +- .../Abstractions/IShardingDbContext.cs | 2 +- .../ISupportShardingTransaction.cs | 16 ++++---- 8 files changed, 49 insertions(+), 52 deletions(-) diff --git a/nuget-publish.bat b/nuget-publish.bat index b5368997..b61d2025 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.4.3.2 -set EFCORE3=3.4.3.2 -set EFCORE5=5.4.3.2 -set EFCORE6=6.4.3.2 +set EFCORE2=2.4.3.3 +set EFCORE3=3.4.3.3 +set EFCORE5=5.4.3.3 +set EFCORE6=6.4.3.3 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs index 610df8cc..7b686069 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs @@ -21,7 +21,7 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources; namespace Samples.AbpSharding { - public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite + public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext, ISupportShardingReadWrite where TDbContext : DbContext { private readonly IShardingDbContextExecutor _shardingDbContextExecutor; diff --git a/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs b/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs index f0e150c7..7a9d54e3 100644 --- a/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs +++ b/src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; +using ShardingCore.Exceptions; using ShardingCore.Sharding.Abstractions; namespace ShardingCore.EFCores @@ -21,26 +22,25 @@ namespace ShardingCore.EFCores */ public class ShardingRelationalTransaction : RelationalTransaction { - private readonly ISupportShardingTransaction _supportShardingTransaction; - private bool supportShardingTransaction => _supportShardingTransaction != null; + private readonly IShardingDbContext _shardingDbContext; #if EFCORE6 - public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned, ISqlGenerationHelper sqlGenerationHelper) : base(connection, transaction, transactionId, logger, transactionOwned, sqlGenerationHelper) + public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned, ISqlGenerationHelper sqlGenerationHelper) : base(connection, transaction, transactionId, logger, transactionOwned, sqlGenerationHelper) { - _supportShardingTransaction = supportShardingTransaction; + _shardingDbContext = shardingDbContext ?? throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif #if EFCORE3 || EFCORE5 - public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned) + public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned) { - _supportShardingTransaction = supportShardingTransaction; + _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif #if EFCORE2 - public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) + public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) { - _supportShardingTransaction = supportShardingTransaction; + _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif @@ -57,15 +57,15 @@ namespace ShardingCore.EFCores public override void Commit() { base.Commit(); - _supportShardingTransaction?.Commit(); - _supportShardingTransaction.NotifyShardingTransaction(); + _shardingDbContext.Commit(); + _shardingDbContext.NotifyShardingTransaction(); } public override void Rollback() { base.Rollback(); - _supportShardingTransaction?.Rollback(); - _supportShardingTransaction.NotifyShardingTransaction(); + _shardingDbContext.Rollback(); + _shardingDbContext.NotifyShardingTransaction(); } #if !EFCORE2 @@ -73,21 +73,17 @@ namespace ShardingCore.EFCores public override async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()) { await base.RollbackAsync(cancellationToken); - if (supportShardingTransaction) - { - await _supportShardingTransaction.RollbackAsync(cancellationToken); - } - _supportShardingTransaction.NotifyShardingTransaction(); + + await _shardingDbContext.RollbackAsync(cancellationToken); + _shardingDbContext.NotifyShardingTransaction(); } public override async Task CommitAsync(CancellationToken cancellationToken = new CancellationToken()) { await base.CommitAsync(cancellationToken); - if (supportShardingTransaction) - { - await _supportShardingTransaction.CommitAsync(cancellationToken); - } - _supportShardingTransaction.NotifyShardingTransaction(); + + await _shardingDbContext.CommitAsync(cancellationToken); + _shardingDbContext.NotifyShardingTransaction(); } #endif } diff --git a/src/ShardingCore/EFCores/ShardingRelationalTransactionFactory.cs b/src/ShardingCore/EFCores/ShardingRelationalTransactionFactory.cs index ac94a4bd..02522926 100644 --- a/src/ShardingCore/EFCores/ShardingRelationalTransactionFactory.cs +++ b/src/ShardingCore/EFCores/ShardingRelationalTransactionFactory.cs @@ -30,8 +30,8 @@ namespace ShardingCore.EFCores public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned) { - var supportShardingTransaction = connection.Context as ISupportShardingTransaction; - return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, transactionId, logger, transactionOwned, this.Dependencies.SqlGenerationHelper); + var shardingDbContext = connection.Context as IShardingDbContext; + return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, transactionId, logger, transactionOwned, this.Dependencies.SqlGenerationHelper); } } #endif @@ -47,8 +47,8 @@ namespace ShardingCore.EFCores public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger logger, bool transactionOwned) { - var supportShardingTransaction = connection.Context as ISupportShardingTransaction; - return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, transactionId, logger, transactionOwned); + var shardingDbContext = connection.Context as IShardingDbContext; + return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, transactionId, logger, transactionOwned); } } #endif @@ -65,8 +65,8 @@ namespace ShardingCore.EFCores , IDiagnosticsLogger logger, bool transactionOwned) { - var supportShardingTransaction = GetDbContext(connection) as ISupportShardingTransaction; - return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, logger, + var shardingDbContext = GetDbContext(connection) as IShardingDbContext; + return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, logger, transactionOwned); } diff --git a/src/ShardingCore/EFCores/ShardingRelationalTransactionManager.cs b/src/ShardingCore/EFCores/ShardingRelationalTransactionManager.cs index 55f5ed5d..75f0b6e4 100644 --- a/src/ShardingCore/EFCores/ShardingRelationalTransactionManager.cs +++ b/src/ShardingCore/EFCores/ShardingRelationalTransactionManager.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; +using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Sharding.Abstractions; @@ -25,12 +26,12 @@ namespace ShardingCore.EFCores public class ShardingRelationalTransactionManager : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext { private readonly IRelationalConnection _relationalConnection; - private readonly ISupportShardingTransaction _supportShardingTransaction; + private readonly IShardingDbContext _shardingDbContext; #if !EFCORE2 public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection) { _relationalConnection = relationalConnection; - _supportShardingTransaction = relationalConnection.Context as ISupportShardingTransaction; + _shardingDbContext = relationalConnection.Context as IShardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } #endif @@ -38,7 +39,7 @@ namespace ShardingCore.EFCores public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection) { _relationalConnection = relationalConnection; - _supportShardingTransaction = GetDbContext(relationalConnection) as ISupportShardingTransaction; + _shardingDbContext = GetDbContext(relationalConnection) as IShardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}"); } private DbContext GetDbContext(IRelationalConnection connection) { @@ -80,7 +81,7 @@ namespace ShardingCore.EFCores public IDbContextTransaction BeginTransaction(IsolationLevel isolationLevel) { var dbContextTransaction = _relationalConnection.BeginTransaction(isolationLevel); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } @@ -88,14 +89,14 @@ namespace ShardingCore.EFCores CancellationToken cancellationToken = new CancellationToken()) { var dbContextTransaction = await _relationalConnection.BeginTransactionAsync(isolationLevel, cancellationToken); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } public IDbContextTransaction UseTransaction(DbTransaction transaction) { var dbContextTransaction = _relationalConnection.UseTransaction(transaction); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } #if !EFCORE2 @@ -108,7 +109,7 @@ namespace ShardingCore.EFCores public async Task UseTransactionAsync(DbTransaction transaction, CancellationToken cancellationToken = new CancellationToken()) { var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, cancellationToken); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } #if !EFCORE3 @@ -124,14 +125,14 @@ namespace ShardingCore.EFCores public IDbContextTransaction UseTransaction(DbTransaction transaction, Guid transactionId) { var dbContextTransaction = _relationalConnection.UseTransaction(transaction, transactionId); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } public async Task UseTransactionAsync(DbTransaction transaction, Guid transactionId, CancellationToken cancellationToken = new CancellationToken()) { var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, transactionId, cancellationToken); - _supportShardingTransaction?.NotifyShardingTransaction(); + _shardingDbContext.NotifyShardingTransaction(); return dbContextTransaction; } #endif diff --git a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs index f793eef0..223c4322 100644 --- a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs +++ b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs @@ -27,7 +27,7 @@ namespace ShardingCore.Sharding /// /// 分表分库的dbcontext /// - public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite//,ICurrentDbContextDiscover + public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingReadWrite//,ICurrentDbContextDiscover { protected IShardingDbContextExecutor ShardingDbContextExecutor { get; } diff --git a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs index 9ae69540..a4e3700e 100644 --- a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs +++ b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs @@ -11,7 +11,7 @@ namespace ShardingCore.Sharding.Abstractions * @Date: Saturday, 14 August 2021 21:47:11 * @Email: 326308290@qq.com */ - public interface IShardingDbContext + public interface IShardingDbContext: IShardingTransaction { /// /// create DbContext diff --git a/src/ShardingCore/Sharding/Abstractions/ISupportShardingTransaction.cs b/src/ShardingCore/Sharding/Abstractions/ISupportShardingTransaction.cs index bad6309a..d6933002 100644 --- a/src/ShardingCore/Sharding/Abstractions/ISupportShardingTransaction.cs +++ b/src/ShardingCore/Sharding/Abstractions/ISupportShardingTransaction.cs @@ -16,15 +16,15 @@ namespace ShardingCore.Sharding.Abstractions * @Ver: 1.0 * @Email: 326308290@qq.com */ - [Obsolete] + [Obsolete("can remove")] public interface ISupportShardingTransaction { - void NotifyShardingTransaction(); - void Rollback(); - void Commit(); -#if !EFCORE2 - Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()); - Task CommitAsync(CancellationToken cancellationToken = new CancellationToken()); -#endif +// void NotifyShardingTransaction(); +// void Rollback(); +// void Commit(); +//#if !EFCORE2 +// Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()); +// Task CommitAsync(CancellationToken cancellationToken = new CancellationToken()); +//#endif } }