支持IShardingRuntimeContext<TDbContext>接口注入,支持多dbcontext分片的处理,修复多次AddShardingDbContext后导致的只有最后一个生效的bug,发布6.8.0.2
This commit is contained in:
parent
048b1481d9
commit
be1ce43765
|
@ -89,7 +89,16 @@ namespace Sample.BulkConsole
|
||||||
|
|
||||||
var b = DateTime.Now.Date.AddDays(-3);
|
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 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();
|
var startNew1 = Stopwatch.StartNew();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
using ShardingCore.Sharding.Abstractions;
|
||||||
|
|
||||||
|
namespace ShardingCore.Core.RuntimeContexts;
|
||||||
|
|
||||||
|
public interface IShardingRuntimeContext<TDbContext>:IShardingRuntimeContext where TDbContext:IShardingDbContext
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -31,7 +31,7 @@ using ShardingCore.TableCreator;
|
||||||
|
|
||||||
namespace ShardingCore.Core.RuntimeContexts
|
namespace ShardingCore.Core.RuntimeContexts
|
||||||
{
|
{
|
||||||
public sealed class ShardingRuntimeContext : IShardingRuntimeContext
|
public sealed class ShardingRuntimeContext<TDbContext> : IShardingRuntimeContext<TDbContext> where TDbContext:IShardingDbContext
|
||||||
{
|
{
|
||||||
private bool isInited = false;
|
private bool isInited = false;
|
||||||
private object INIT_LOCK = new object();
|
private object INIT_LOCK = new object();
|
||||||
|
@ -42,12 +42,9 @@ namespace ShardingCore.Core.RuntimeContexts
|
||||||
private IServiceCollection _serviceMap = new ServiceCollection();
|
private IServiceCollection _serviceMap = new ServiceCollection();
|
||||||
|
|
||||||
private IServiceProvider _serviceProvider;
|
private IServiceProvider _serviceProvider;
|
||||||
public Type DbContextType { get; }
|
|
||||||
// private ILoggerFactory _applicationLoggerFactory;
|
public Type DbContextType => typeof(TDbContext);
|
||||||
public ShardingRuntimeContext(Type dbContextType)
|
|
||||||
{
|
|
||||||
DbContextType = dbContextType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddServiceConfig(Action<IServiceCollection> configure)
|
public void AddServiceConfig(Action<IServiceCollection> configure)
|
||||||
{
|
{
|
||||||
|
|
|
@ -79,11 +79,12 @@ namespace ShardingCore.Core.ShardingConfigurations.ConfigBuilders
|
||||||
[Obsolete("plz use AddShardingCore")]
|
[Obsolete("plz use AddShardingCore")]
|
||||||
public void EnsureConfig()
|
public void EnsureConfig()
|
||||||
{
|
{
|
||||||
_services.AddSingleton<IShardingRuntimeContext>(sp => _shardingRuntimeBuilder.Build(sp));
|
AddShardingCore();
|
||||||
}
|
}
|
||||||
public void 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>>());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,14 +106,14 @@ namespace ShardingCore
|
||||||
public static void UseDefaultSharding<TShardingDbContext>(this DbContextOptionsBuilder dbContextOptionsBuilder,
|
public static void UseDefaultSharding<TShardingDbContext>(this DbContextOptionsBuilder dbContextOptionsBuilder,
|
||||||
IServiceProvider serviceProvider) where TShardingDbContext : DbContext, IShardingDbContext
|
IServiceProvider serviceProvider) where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
{
|
{
|
||||||
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
|
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext<TShardingDbContext>>();
|
||||||
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void UseDefaultSharding<TShardingDbContext>(IServiceProvider serviceProvider,
|
public static void UseDefaultSharding<TShardingDbContext>(IServiceProvider serviceProvider,
|
||||||
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
{
|
{
|
||||||
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
|
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext<TShardingDbContext>>();
|
||||||
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,18 +71,18 @@ namespace ShardingCore
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IShardingRuntimeContext Build()
|
public IShardingRuntimeContext<TShardingDbContext> Build()
|
||||||
{
|
{
|
||||||
return Build(null);
|
return Build(null);
|
||||||
}
|
}
|
||||||
public IShardingRuntimeContext Build(IServiceProvider appServiceProvider)
|
public IShardingRuntimeContext<TShardingDbContext> Build(IServiceProvider appServiceProvider)
|
||||||
{
|
{
|
||||||
return Build(appServiceProvider, appServiceProvider?.GetService<ILoggerFactory>());
|
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 =>
|
shardingRuntimeContext.AddServiceConfig(services =>
|
||||||
{
|
{
|
||||||
// services.AddSingleton<IDbContextTypeCollector>(sp => new DbContextTypeCollector<TShardingDbContext>());
|
// services.AddSingleton<IDbContextTypeCollector>(sp => new DbContextTypeCollector<TShardingDbContext>());
|
||||||
|
|
|
@ -174,12 +174,12 @@ namespace ShardingCore.Test
|
||||||
|
|
||||||
await _virtualDbContext.AddRangeAsync(logDays);
|
await _virtualDbContext.AddRangeAsync(logDays);
|
||||||
var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area));
|
var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area));
|
||||||
|
|
||||||
Assert.Equal(2, bulkShardingExpression.Count);
|
Assert.Equal(2, bulkShardingExpression.Count);
|
||||||
Assert.True(bulkShardingExpression.ContainsKey("A"));
|
Assert.True(bulkShardingExpression.ContainsKey("A"));
|
||||||
Assert.True(bulkShardingExpression.ContainsKey("B"));
|
Assert.True(bulkShardingExpression.ContainsKey("B"));
|
||||||
|
|
||||||
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
|
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
|
||||||
|
|
||||||
Assert.Equal(1, bulkShardingTableExpression.Count());
|
Assert.Equal(1, bulkShardingTableExpression.Count());
|
||||||
|
|
||||||
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
|
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
|
||||||
|
|
Loading…
Reference in New Issue