添加读写分离的写数据库只读还是读读数据库扩展方法

This commit is contained in:
xuejiaming 2021-10-05 14:08:55 +08:00
parent 65aef115cb
commit 2a9d3fff98
6 changed files with 79 additions and 26 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Text;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
namespace ShardingCore.Extensions
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/10/5 8:37:31
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public static class ShardingReadWriteExtension
{
/// <summary>
/// 设置读写分离读取写数据库
/// </summary>
/// <param name="shardingDbContext"></param>
/// <returns></returns>
public static bool ReadWriteSeparationWriteOnly(this IShardingDbContext shardingDbContext)
{
if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite)
{
supportShardingReadWrite.SetReadWriteSeparation(false);
return true;
}
return false;
}
/// <summary>
/// 设置读写分离读读数据库
/// </summary>
/// <param name="shardingDbContext"></param>
/// <returns></returns>
public static bool ReadWriteSeparationReadOnly(this IShardingDbContext shardingDbContext)
{
if (shardingDbContext is ISupportShardingReadWrite supportShardingReadWrite)
{
supportShardingReadWrite.SetReadWriteSeparation(true);
return true;
}
return false;
}
/// <summary>
/// 设置读写分离
/// </summary>
/// <param name="supportShardingReadWrite"></param>
/// <param name="readOnly">是否是读数据源</param>
private static void SetReadWriteSeparation(this ISupportShardingReadWrite supportShardingReadWrite, bool readOnly)
{
var shardingReadWriteManager = ShardingContainer.GetService<IShardingReadWriteManager>();
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(supportShardingReadWrite.GetType());
if (shardingReadWriteContext != null)
{
if (shardingReadWriteContext.DefaultPriority > supportShardingReadWrite.ReadWriteSeparationPriority)
{
supportShardingReadWrite.ReadWriteSeparationPriority = shardingReadWriteContext.DefaultPriority + 1;
}
}
if (supportShardingReadWrite.ReadWriteSeparation!= readOnly)
{
supportShardingReadWrite.ReadWriteSeparation = readOnly;
}
}
}
}

View File

@ -38,7 +38,9 @@ namespace ShardingCore.Sharding
typeof(ShardingDbContextExecutor<,>).GetGenericType1(this.GetType(), ActualDbContextType));
}
/// <summary>
/// 正真执行的dbcontext类型
/// </summary>
public Type ActualDbContextType { get; }
/// <summary>
/// 读写分离优先级
@ -71,14 +73,6 @@ namespace ShardingCore.Sharding
{
return _shardingDbContextExecutor.CreateGenericDbContext(entity);
}
/// <summary>
/// 是否启用了读写分离
/// </summary>
/// <returns></returns>
public bool IsUseReadWriteSeparation()
{
return _shardingDbContextExecutor.IsUseReadWriteSeparation();
}
public override EntityEntry Add(object entity)

View File

@ -31,11 +31,6 @@ namespace ShardingCore.Sharding.Abstractions
/// <param name="entity"></param>
/// <returns></returns>
DbContext CreateGenericDbContext<T>(T entity) where T : class;
/// <summary>
/// 是否启用了读写分离
/// </summary>
/// <returns></returns>
bool IsUseReadWriteSeparation();
}

View File

@ -38,11 +38,6 @@ namespace ShardingCore.Sharding.Abstractions
/// </summary>
bool ReadWriteSeparation { get; set; }
/// <summary>
/// 是否使用了读写分离
/// </summary>
/// <returns></returns>
bool IsUseReadWriteSeparation();
/// <summary>
/// create sharding db context options

View File

@ -117,11 +117,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
return new ShardingDbContextOptions(CreateParallelDbContextOptions(dataSourceName), routeTail);
}
public bool IsUseReadWriteSeparation()
{
return _actualConnectionStringManager.IsUseReadWriteSeparation();
}
public DbContext CreateDbContext(bool parallelQuery, string dataSourceName, IRouteTail routeTail)
{

View File

@ -197,10 +197,13 @@ namespace ShardingCore.Sharding
{
return IsCrossDataSource || IsCrossTable;
}
/// <summary>
/// 是否启用读写分离
/// </summary>
/// <returns></returns>
private bool IsUseReadWriteSeparation()
{
return _shardingDbContext.IsUseReadWriteSeparation();
return _shardingConfigOption.UseReadWrite;
}
/// <summary>