移除对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
::定义版本
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

View File

@ -21,7 +21,7 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
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
{
private readonly IShardingDbContextExecutor _shardingDbContextExecutor;

View File

@ -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<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
#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
#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
@ -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
}

View File

@ -30,8 +30,8 @@ namespace ShardingCore.EFCores
public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId,
IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> 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<DbLoggerCategory.Database.Transaction> 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<DbLoggerCategory.Database.Transaction> 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);
}

View File

@ -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<TShardingDbContext> : 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<IDbContextTransaction> 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<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, Guid transactionId,
CancellationToken cancellationToken = new CancellationToken())
{
var dbContextTransaction = await _relationalConnection.UseTransactionAsync(transaction, transactionId, cancellationToken);
_supportShardingTransaction?.NotifyShardingTransaction();
_shardingDbContext.NotifyShardingTransaction();
return dbContextTransaction;
}
#endif

View File

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

View File

@ -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
{
/// <summary>
/// create DbContext

View File

@ -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
}
}