支持IShardingRuntimeContext<TDbContext>接口注入,支持多dbcontext分片的处理,修复多次AddShardingDbContext后导致的只有最后一个生效的bug,发布6.8.0.2

This commit is contained in:
xuejiaming 2022-10-26 10:09:14 +08:00
parent 048b1481d9
commit be1ce43765
7 changed files with 32 additions and 17 deletions

View File

@ -89,7 +89,16 @@ namespace Sample.BulkConsole
var b = DateTime.Now.Date.AddDays(-3);
var queryable = myShardingDbContext.Set<Order>().Select(o=>new {Id=o.Id,OrderNo=o.OrderNo, CreateTime =o.CreateTime });//.Where(o => o.CreateTime >= b);
var dateTime = DateTime.Now;
var dbContexts = myShardingDbContext.BulkShardingTableExpression<MyShardingDbContext,Order>(o=>o.CreateTime<=dateTime);
using (var dbContextTransaction = myShardingDbContext.Database.BeginTransaction())
{
foreach (var dbContext in dbContexts)
{
dbContext.Set<Order>().Where(o=>o.CreateTime<=dateTime).BatchUpdate(a => new Order { OrderNo = "12345" });
}
dbContextTransaction.Commit();
}
var startNew1 = Stopwatch.StartNew();

View File

@ -0,0 +1,8 @@
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Core.RuntimeContexts;
public interface IShardingRuntimeContext<TDbContext>:IShardingRuntimeContext where TDbContext:IShardingDbContext
{
}

View File

@ -31,7 +31,7 @@ using ShardingCore.TableCreator;
namespace ShardingCore.Core.RuntimeContexts
{
public sealed class ShardingRuntimeContext : IShardingRuntimeContext
public sealed class ShardingRuntimeContext<TDbContext> : IShardingRuntimeContext<TDbContext> where TDbContext:IShardingDbContext
{
private bool isInited = false;
private object INIT_LOCK = new object();
@ -42,12 +42,9 @@ namespace ShardingCore.Core.RuntimeContexts
private IServiceCollection _serviceMap = new ServiceCollection();
private IServiceProvider _serviceProvider;
public Type DbContextType { get; }
// private ILoggerFactory _applicationLoggerFactory;
public ShardingRuntimeContext(Type dbContextType)
{
DbContextType = dbContextType;
}
public Type DbContextType => typeof(TDbContext);
public void AddServiceConfig(Action<IServiceCollection> configure)
{

View File

@ -79,11 +79,12 @@ namespace ShardingCore.Core.ShardingConfigurations.ConfigBuilders
[Obsolete("plz use AddShardingCore")]
public void EnsureConfig()
{
_services.AddSingleton<IShardingRuntimeContext>(sp => _shardingRuntimeBuilder.Build(sp));
AddShardingCore();
}
public void AddShardingCore()
{
_services.AddSingleton<IShardingRuntimeContext>(sp => _shardingRuntimeBuilder.Build(sp));
_services.AddSingleton<IShardingRuntimeContext<TShardingDbContext>>(sp => _shardingRuntimeBuilder.Build(sp));
_services.AddSingleton<IShardingRuntimeContext>(sp => sp.GetService<IShardingRuntimeContext<TShardingDbContext>>());
}
}

View File

@ -106,14 +106,14 @@ namespace ShardingCore
public static void UseDefaultSharding<TShardingDbContext>(this DbContextOptionsBuilder dbContextOptionsBuilder,
IServiceProvider serviceProvider) where TShardingDbContext : DbContext, IShardingDbContext
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext<TShardingDbContext>>();
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
}
public static void UseDefaultSharding<TShardingDbContext>(IServiceProvider serviceProvider,
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext<TShardingDbContext>>();
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
}

View File

@ -71,18 +71,18 @@ namespace ShardingCore
return this;
}
public IShardingRuntimeContext Build()
public IShardingRuntimeContext<TShardingDbContext> Build()
{
return Build(null);
}
public IShardingRuntimeContext Build(IServiceProvider appServiceProvider)
public IShardingRuntimeContext<TShardingDbContext> Build(IServiceProvider appServiceProvider)
{
return Build(appServiceProvider, appServiceProvider?.GetService<ILoggerFactory>());
}
public IShardingRuntimeContext Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory)
public IShardingRuntimeContext<TShardingDbContext> Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory)
{
var shardingRuntimeContext = new ShardingRuntimeContext(typeof(TShardingDbContext));
var shardingRuntimeContext = new ShardingRuntimeContext<TShardingDbContext>();
shardingRuntimeContext.AddServiceConfig(services =>
{
// services.AddSingleton<IDbContextTypeCollector>(sp => new DbContextTypeCollector<TShardingDbContext>());

View File

@ -174,12 +174,12 @@ namespace ShardingCore.Test
await _virtualDbContext.AddRangeAsync(logDays);
var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area));
Assert.Equal(2, bulkShardingExpression.Count);
Assert.True(bulkShardingExpression.ContainsKey("A"));
Assert.True(bulkShardingExpression.ContainsKey("B"));
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
Assert.Equal(1, bulkShardingTableExpression.Count());
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");