添加use shell dbcontext option builder单独配置,将innerdbcontextoptionbuilder改名成executordbcontextoptionbuilder

This commit is contained in:
xuejiaming 2022-04-20 08:47:54 +08:00
parent 3b256534c4
commit 19aeb360fb
9 changed files with 74 additions and 23 deletions

View File

@ -60,7 +60,12 @@ namespace Sample.MultiConfig.Controllers
return dbContextOptionsBuilder;
}
public override void UseInnerDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
public override void UseShellDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
{
}
public override void UseExecutorDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
{
}

View File

@ -103,7 +103,8 @@ namespace ShardingCore.Core.ShardingConfigurations.Abstractions
/// DbContext如何通过连接字符串创建
/// </summary>
Action<string, DbContextOptionsBuilder> ConnectionStringConfigure { get; }
Action<DbContextOptionsBuilder> InnerDbContextConfigure { get; }
Action<DbContextOptionsBuilder> ExecutorDbContextConfigure { get; }
Action<DbContextOptionsBuilder> ShellDbContextConfigure { get; }
/// <summary>
/// DbContext如何通过连接字符串创建
@ -114,6 +115,7 @@ namespace ShardingCore.Core.ShardingConfigurations.Abstractions
/// </summary>
public void UseShardingTransaction(Action<DbConnection, DbContextOptionsBuilder> transactionConfigure);
void UseInnerDbContextConfigure(Action<DbContextOptionsBuilder> innerDbContextConfigure);
void UseExecutorDbContextConfigure(Action<DbContextOptionsBuilder> executorDbContextConfigure);
void UseShellDbContextConfigure(Action<DbContextOptionsBuilder> shellDbContextConfigure);
}
}

View File

@ -95,9 +95,13 @@ namespace ShardingCore.Core.ShardingConfigurations
/// </summary>
public Action<string, DbContextOptionsBuilder> ConnectionStringConfigure { get; private set; }
/// <summary>
/// 仅内部DbContext生效的配置委托
/// 外部dbcontext的配置委托
/// </summary>
public Action<DbContextOptionsBuilder> InnerDbContextConfigure { get; private set; }
public Action<DbContextOptionsBuilder> ShellDbContextConfigure { get; private set; }
/// <summary>
/// 仅内部真正执行的DbContext生效的配置委托
/// </summary>
public Action<DbContextOptionsBuilder> ExecutorDbContextConfigure { get; private set; }
/// <summary>
/// 如何使用字符串创建DbContext
/// </summary>
@ -117,13 +121,22 @@ namespace ShardingCore.Core.ShardingConfigurations
ConnectionConfigure = transactionConfigure ?? throw new ArgumentNullException(nameof(transactionConfigure));
}
/// <summary>
/// 仅内部DbContext生效,作为最外面的壳DbContext将不会生效
/// 仅内部正真执行DbContext生效,作为最外面的壳DbContext将不会生效
/// </summary>
/// <param name="innerDbContextConfigure"></param>
/// <param name="executorDbContextConfigure"></param>
/// <exception cref="ArgumentNullException"></exception>
public void UseInnerDbContextConfigure(Action<DbContextOptionsBuilder> innerDbContextConfigure)
public void UseExecutorDbContextConfigure(Action<DbContextOptionsBuilder> executorDbContextConfigure)
{
InnerDbContextConfigure= innerDbContextConfigure?? throw new ArgumentNullException(nameof(innerDbContextConfigure));
ExecutorDbContextConfigure= executorDbContextConfigure ?? throw new ArgumentNullException(nameof(executorDbContextConfigure));
}
/// <summary>
/// 仅外部DbContext生效
/// </summary>
/// <param name="shellDbContextConfigure"></param>
/// <exception cref="ArgumentNullException"></exception>
public void UseShellDbContextConfigure(Action<DbContextOptionsBuilder> shellDbContextConfigure)
{
ShellDbContextConfigure = shellDbContextConfigure ?? throw new ArgumentNullException(nameof(shellDbContextConfigure));
}
public Func<IServiceProvider, IShardingComparer> ReplaceShardingComparerFactory { get; private set; } = sp => new CSharpLanguageShardingComparer();
/// <summary>

View File

@ -162,7 +162,8 @@ namespace ShardingCore.Core.ShardingConfigurations
/// <summary>
/// 仅内部DbContext生效的配置委托
/// </summary>
public Action<DbContextOptionsBuilder> InnerDbContextConfigure { get; private set; }
public Action<DbContextOptionsBuilder> ExecutorDbContextConfigure { get; private set; }
public Action<DbContextOptionsBuilder> ShellDbContextConfigure { get; private set; }
/// <summary>
@ -182,11 +183,16 @@ namespace ShardingCore.Core.ShardingConfigurations
/// <summary>
/// 仅内部真实DbContext配置的方法
/// </summary>
/// <param name="innerDbContextConfigure"></param>
/// <param name="executorDbContextConfigure"></param>
/// <exception cref="ArgumentNullException"></exception>
public void UseInnerDbContextConfigure(Action<DbContextOptionsBuilder> innerDbContextConfigure)
public void UseExecutorDbContextConfigure(Action<DbContextOptionsBuilder> executorDbContextConfigure)
{
InnerDbContextConfigure = innerDbContextConfigure ?? throw new ArgumentNullException(nameof(innerDbContextConfigure));
ExecutorDbContextConfigure = executorDbContextConfigure ?? throw new ArgumentNullException(nameof(executorDbContextConfigure));
}
public void UseShellDbContextConfigure(Action<DbContextOptionsBuilder> shellDbContextConfigure)
{
ShellDbContextConfigure = shellDbContextConfigure ?? throw new ArgumentNullException(nameof(shellDbContextConfigure));
}
}
}

View File

@ -46,7 +46,9 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
public abstract DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection,
DbContextOptionsBuilder dbContextOptionsBuilder);
public abstract void UseInnerDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
public abstract void UseShellDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
public abstract void UseExecutorDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
public virtual bool UseReadWriteSeparation()
{

View File

@ -74,10 +74,15 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
/// <returns></returns>
DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder);
/// <summary>
/// 外部db context
/// </summary>
/// <param name="dbContextOptionsBuilder"></param>
void UseShellDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
/// <summary>
/// 真实DbContextOptionBuilder的配置
/// </summary>
/// <param name="dbContextOptionsBuilder"></param>
void UseInnerDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
void UseExecutorDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder);
/// <summary>
/// 使用读写分离
/// </summary>

View File

@ -97,20 +97,37 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
return dbContextOptionsBuilder;
}
public override void UseInnerDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
public override void UseShellDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
{
if (_options.InnerDbContextConfigure == null && _shardingEntityConfigOptions.InnerDbContextConfigure == null)
if (_options.ShellDbContextConfigure == null && _shardingEntityConfigOptions.ShellDbContextConfigure == null)
{
return;
}
if (_options.InnerDbContextConfigure != null)
if (_options.ShellDbContextConfigure != null)
{
_options.InnerDbContextConfigure.Invoke(dbContextOptionsBuilder);
_options.ShellDbContextConfigure.Invoke(dbContextOptionsBuilder);
}
else
{
_shardingEntityConfigOptions.InnerDbContextConfigure?.Invoke(dbContextOptionsBuilder);
_shardingEntityConfigOptions.ShellDbContextConfigure?.Invoke(dbContextOptionsBuilder);
}
}
public override void UseExecutorDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder)
{
if (_options.ExecutorDbContextConfigure == null && _shardingEntityConfigOptions.ExecutorDbContextConfigure == null)
{
return;
}
if (_options.ExecutorDbContextConfigure != null)
{
_options.ExecutorDbContextConfigure.Invoke(dbContextOptionsBuilder);
}
else
{
_shardingEntityConfigOptions.ExecutorDbContextConfigure?.Invoke(dbContextOptionsBuilder);
}
}
}

View File

@ -228,7 +228,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
{
var doUseDbContextOptionsBuilder = ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder);
doUseDbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>();
ConfigurationParams.UseInnerDbContextOptionBuilder(dbContextOptionsBuilder);
ConfigurationParams.UseExecutorDbContextOptionBuilder(dbContextOptionsBuilder);
return doUseDbContextOptionsBuilder;
}
@ -237,7 +237,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
{
var doUseDbContextOptionsBuilder = ConfigurationParams.UseDbContextOptionsBuilder(dbConnection, dbContextOptionsBuilder);
doUseDbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>();
ConfigurationParams.UseInnerDbContextOptionBuilder(dbContextOptionsBuilder);
ConfigurationParams.UseExecutorDbContextOptionBuilder(dbContextOptionsBuilder);
return doUseDbContextOptionsBuilder;
}

View File

@ -87,7 +87,8 @@ namespace ShardingCore
{
var virtualDataSource = serviceProvider.GetRequiredService<IVirtualDataSourceManager<TShardingDbContext>>().GetCurrentVirtualDataSource();
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
virtualDataSource.ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder).UseSharding<TShardingDbContext>();
var contextOptionsBuilder = virtualDataSource.ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder).UseSharding<TShardingDbContext>();
virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder);
}
internal static IServiceCollection AddInternalShardingCore<TShardingDbContext>(this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext
{