移除对ISupportShardingTransaction的依赖直接合并到IShardingDBContext并且发布x.4.3.3

This commit is contained in:
xuejiaming 2022-05-13 17:03:58 +08:00
parent 58ee2a7ba3
commit ab712361e7
8 changed files with 49 additions and 52 deletions

View File

@ -1,9 +1,9 @@
:start :start
::定义版本 ::定义版本
set EFCORE2=2.4.3.2 set EFCORE2=2.4.3.3
set EFCORE3=3.4.3.2 set EFCORE3=3.4.3.3
set EFCORE5=5.4.3.2 set EFCORE5=5.4.3.3
set EFCORE6=6.4.3.2 set EFCORE6=6.4.3.3
::删除所有bin与obj下的文件 ::删除所有bin与obj下的文件
@echo off @echo off

View File

@ -21,7 +21,7 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
namespace Samples.AbpSharding namespace Samples.AbpSharding
{ {
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext, ISupportShardingReadWrite
where TDbContext : DbContext where TDbContext : DbContext
{ {
private readonly IShardingDbContextExecutor _shardingDbContextExecutor; private readonly IShardingDbContextExecutor _shardingDbContextExecutor;

View File

@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.EFCores namespace ShardingCore.EFCores
@ -21,26 +22,25 @@ namespace ShardingCore.EFCores
*/ */
public class ShardingRelationalTransaction : RelationalTransaction public class ShardingRelationalTransaction : RelationalTransaction
{ {
private readonly ISupportShardingTransaction _supportShardingTransaction; private readonly IShardingDbContext _shardingDbContext;
private bool supportShardingTransaction => _supportShardingTransaction != null;
#if EFCORE6 #if EFCORE6
public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned, ISqlGenerationHelper sqlGenerationHelper) : base(connection, transaction, transactionId, logger, transactionOwned, sqlGenerationHelper) public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned, ISqlGenerationHelper sqlGenerationHelper) : base(connection, transaction, transactionId, logger, transactionOwned, sqlGenerationHelper)
{ {
_supportShardingTransaction = supportShardingTransaction; _shardingDbContext = shardingDbContext ?? throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}");
} }
#endif #endif
#if EFCORE3 || EFCORE5 #if EFCORE3 || EFCORE5
public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned) public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) : base(connection, transaction, transactionId, logger, transactionOwned)
{ {
_supportShardingTransaction = supportShardingTransaction; _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}");
} }
#endif #endif
#if EFCORE2 #if EFCORE2
public ShardingRelationalTransaction(ISupportShardingTransaction supportShardingTransaction, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned) public ShardingRelationalTransaction(IShardingDbContext shardingDbContext, IRelationalConnection connection, DbTransaction transaction,IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) : base(connection, transaction, logger, transactionOwned)
{ {
_supportShardingTransaction = supportShardingTransaction; _shardingDbContext = shardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}");
} }
#endif #endif
@ -57,15 +57,15 @@ namespace ShardingCore.EFCores
public override void Commit() public override void Commit()
{ {
base.Commit(); base.Commit();
_supportShardingTransaction?.Commit(); _shardingDbContext.Commit();
_supportShardingTransaction.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
} }
public override void Rollback() public override void Rollback()
{ {
base.Rollback(); base.Rollback();
_supportShardingTransaction?.Rollback(); _shardingDbContext.Rollback();
_supportShardingTransaction.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
} }
#if !EFCORE2 #if !EFCORE2
@ -73,21 +73,17 @@ namespace ShardingCore.EFCores
public override async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()) public override async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken())
{ {
await base.RollbackAsync(cancellationToken); await base.RollbackAsync(cancellationToken);
if (supportShardingTransaction)
{ await _shardingDbContext.RollbackAsync(cancellationToken);
await _supportShardingTransaction.RollbackAsync(cancellationToken); _shardingDbContext.NotifyShardingTransaction();
}
_supportShardingTransaction.NotifyShardingTransaction();
} }
public override async Task CommitAsync(CancellationToken cancellationToken = new CancellationToken()) public override async Task CommitAsync(CancellationToken cancellationToken = new CancellationToken())
{ {
await base.CommitAsync(cancellationToken); await base.CommitAsync(cancellationToken);
if (supportShardingTransaction)
{ await _shardingDbContext.CommitAsync(cancellationToken);
await _supportShardingTransaction.CommitAsync(cancellationToken); _shardingDbContext.NotifyShardingTransaction();
}
_supportShardingTransaction.NotifyShardingTransaction();
} }
#endif #endif
} }

View File

@ -30,8 +30,8 @@ namespace ShardingCore.EFCores
public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId,
IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned)
{ {
var supportShardingTransaction = connection.Context as ISupportShardingTransaction; var shardingDbContext = connection.Context as IShardingDbContext;
return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, transactionId, logger, transactionOwned, this.Dependencies.SqlGenerationHelper); return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, transactionId, logger, transactionOwned, this.Dependencies.SqlGenerationHelper);
} }
} }
#endif #endif
@ -47,8 +47,8 @@ namespace ShardingCore.EFCores
public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId,
IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned)
{ {
var supportShardingTransaction = connection.Context as ISupportShardingTransaction; var shardingDbContext = connection.Context as IShardingDbContext;
return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, transactionId, logger, transactionOwned); return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, transactionId, logger, transactionOwned);
} }
} }
#endif #endif
@ -65,8 +65,8 @@ namespace ShardingCore.EFCores
, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, , IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger,
bool transactionOwned) bool transactionOwned)
{ {
var supportShardingTransaction = GetDbContext(connection) as ISupportShardingTransaction; var shardingDbContext = GetDbContext(connection) as IShardingDbContext;
return new ShardingRelationalTransaction(supportShardingTransaction, connection, transaction, logger, return new ShardingRelationalTransaction(shardingDbContext, connection, transaction, logger,
transactionOwned); transactionOwned);
} }

View File

@ -7,6 +7,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
@ -25,12 +26,12 @@ namespace ShardingCore.EFCores
public class ShardingRelationalTransactionManager<TShardingDbContext> : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext public class ShardingRelationalTransactionManager<TShardingDbContext> : IRelationalTransactionManager where TShardingDbContext : DbContext, IShardingDbContext
{ {
private readonly IRelationalConnection _relationalConnection; private readonly IRelationalConnection _relationalConnection;
private readonly ISupportShardingTransaction _supportShardingTransaction; private readonly IShardingDbContext _shardingDbContext;
#if !EFCORE2 #if !EFCORE2
public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection) public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection)
{ {
_relationalConnection = relationalConnection; _relationalConnection = relationalConnection;
_supportShardingTransaction = relationalConnection.Context as ISupportShardingTransaction; _shardingDbContext = relationalConnection.Context as IShardingDbContext??throw new ShardingCoreInvalidOperationException($"should implement {nameof(IShardingDbContext)}");
} }
#endif #endif
@ -38,7 +39,7 @@ namespace ShardingCore.EFCores
public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection) public ShardingRelationalTransactionManager(IRelationalConnection relationalConnection)
{ {
_relationalConnection = 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) private DbContext GetDbContext(IRelationalConnection connection)
{ {
@ -80,7 +81,7 @@ namespace ShardingCore.EFCores
public IDbContextTransaction BeginTransaction(IsolationLevel isolationLevel) public IDbContextTransaction BeginTransaction(IsolationLevel isolationLevel)
{ {
var dbContextTransaction = _relationalConnection.BeginTransaction(isolationLevel); var dbContextTransaction = _relationalConnection.BeginTransaction(isolationLevel);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
@ -88,14 +89,14 @@ namespace ShardingCore.EFCores
CancellationToken cancellationToken = new CancellationToken()) CancellationToken cancellationToken = new CancellationToken())
{ {
var dbContextTransaction = await _relationalConnection.BeginTransactionAsync(isolationLevel, cancellationToken); var dbContextTransaction = await _relationalConnection.BeginTransactionAsync(isolationLevel, cancellationToken);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
public IDbContextTransaction UseTransaction(DbTransaction transaction) public IDbContextTransaction UseTransaction(DbTransaction transaction)
{ {
var dbContextTransaction = _relationalConnection.UseTransaction(transaction); var dbContextTransaction = _relationalConnection.UseTransaction(transaction);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
#if !EFCORE2 #if !EFCORE2
@ -108,7 +109,7 @@ namespace ShardingCore.EFCores
public async Task<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, CancellationToken cancellationToken = new CancellationToken()) public async Task<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, CancellationToken cancellationToken = new CancellationToken())
{ {
var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, cancellationToken); var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, cancellationToken);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
#if !EFCORE3 #if !EFCORE3
@ -124,14 +125,14 @@ namespace ShardingCore.EFCores
public IDbContextTransaction UseTransaction(DbTransaction transaction, Guid transactionId) public IDbContextTransaction UseTransaction(DbTransaction transaction, Guid transactionId)
{ {
var dbContextTransaction = _relationalConnection.UseTransaction(transaction, transactionId); var dbContextTransaction = _relationalConnection.UseTransaction(transaction, transactionId);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
public async Task<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, Guid transactionId, public async Task<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, Guid transactionId,
CancellationToken cancellationToken = new CancellationToken()) CancellationToken cancellationToken = new CancellationToken())
{ {
var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, transactionId, cancellationToken); var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, transactionId, cancellationToken);
_supportShardingTransaction?.NotifyShardingTransaction(); _shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction; return dbContextTransaction;
} }
#endif #endif

View File

@ -27,7 +27,7 @@ namespace ShardingCore.Sharding
/// <summary> /// <summary>
/// 分表分库的dbcontext /// 分表分库的dbcontext
/// </summary> /// </summary>
public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite//,ICurrentDbContextDiscover public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingReadWrite//,ICurrentDbContextDiscover
{ {
protected IShardingDbContextExecutor ShardingDbContextExecutor { get; } protected IShardingDbContextExecutor ShardingDbContextExecutor { get; }

View File

@ -11,7 +11,7 @@ namespace ShardingCore.Sharding.Abstractions
* @Date: Saturday, 14 August 2021 21:47:11 * @Date: Saturday, 14 August 2021 21:47:11
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public interface IShardingDbContext public interface IShardingDbContext: IShardingTransaction
{ {
/// <summary> /// <summary>
/// create DbContext /// create DbContext

View File

@ -16,15 +16,15 @@ namespace ShardingCore.Sharding.Abstractions
* @Ver: 1.0 * @Ver: 1.0
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
[Obsolete] [Obsolete("can remove")]
public interface ISupportShardingTransaction public interface ISupportShardingTransaction
{ {
void NotifyShardingTransaction(); // void NotifyShardingTransaction();
void Rollback(); // void Rollback();
void Commit(); // void Commit();
#if !EFCORE2 //#if !EFCORE2
Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()); // Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken());
Task CommitAsync(CancellationToken cancellationToken = new CancellationToken()); // Task CommitAsync(CancellationToken cancellationToken = new CancellationToken());
#endif //#endif
} }
} }