Skip to content

Commit 244bf06

Browse files
committed
添加dbcontext类型发现接口用来判断如果存在分表路由那么必须是分表数据库上下文[#182],将多个接口合并到IShardingDbContext接口上,并且默认的ShellDbContext接口支持track的直接调用
1 parent c528a4a commit 244bf06

18 files changed

+135
-167
lines changed

samples/Sample.MySql/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void ConfigureServices(IServiceCollection services)
131131
{
132132
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
133133
});
134-
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>()
134+
})
135135
.AddShardingCore();
136136
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
137137
// .UseMutliDefaultSharding<DefaultShardingDbContext>);

samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace Samples.AbpSharding
2323
{
2424

25-
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext, ISupportShardingReadWrite
25+
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext
2626
where TDbContext : DbContext
2727
{
2828
private readonly IShardingDbContextExecutor _shardingDbContextExecutor;
@@ -571,5 +571,9 @@ public IVirtualDataSource GetVirtualDataSource()
571571
return _shardingDbContextExecutor.GetVirtualDataSource();
572572
}
573573

574+
public IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts()
575+
{
576+
return _shardingDbContextExecutor.GetCurrentDbContexts();
577+
}
574578
}
575579
}

samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ namespace Samples.AbpSharding
2828
{
2929
public abstract class AbstractShardingAbpZeroDbContext<TTenant, TRole, TUser, TSelf>
3030
: AbpZeroDbContext<TTenant, TRole, TUser, TSelf>,
31-
IShardingDbContext,
32-
ISupportShardingReadWrite,
33-
IShardingTableDbContext
31+
IShardingDbContext
3432
where TTenant : AbpTenant<TUser>
3533
where TRole : AbpRole<TUser>
3634
where TUser : AbpUser<TUser>
@@ -650,5 +648,9 @@ protected virtual void FillDbContextInject(DbContext dbContext)
650648

651649
#endregion
652650

651+
public IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts()
652+
{
653+
return _shardingDbContextExecutor.GetCurrentDbContexts();
654+
}
653655
}
654656
}

src/ShardingCore/Bootstrappers/ShardingInitializer.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using System.Threading.Tasks;
33
using Microsoft.Extensions.Logging;
44
using ShardingCore.Core.DbContextCreator;
5+
using ShardingCore.Core.DbContextTypeAwares;
56
using ShardingCore.Core.EntityMetadatas;
67
using ShardingCore.Core.ServiceProviders;
78
using ShardingCore.Core.ShardingConfigurations.Abstractions;
89
using ShardingCore.Exceptions;
910
using ShardingCore.Extensions;
1011
using ShardingCore.Jobs;
1112
using ShardingCore.Jobs.Abstaractions;
12-
13+
using ShardingCore.Sharding.Abstractions;
1314
using ShardingCore.Sharding.MergeEngines.ParallelControl;
1415
using ShardingCore.Sharding.ParallelTables;
1516

@@ -20,18 +21,25 @@ internal class ShardingInitializer:IShardingInitializer
2021
{
2122
private readonly ILogger<ShardingBootstrapper> _logger;
2223
private readonly IShardingProvider _shardingProvider;
24+
private readonly IDbContextTypeAware _dbContextTypeAware;
2325
private readonly IShardingRouteConfigOptions _routeConfigOptions;
2426
private readonly IEntityMetadataManager _entityMetadataManager;
2527
private readonly IParallelTableManager _parallelTableManager;
2628
private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce();
2729

28-
public ShardingInitializer(IShardingProvider shardingProvider,ILogger<ShardingBootstrapper> logger)
30+
public ShardingInitializer(IShardingProvider shardingProvider,
31+
IDbContextTypeAware dbContextTypeAware,
32+
IShardingRouteConfigOptions shardingRouteConfigOptions,
33+
IEntityMetadataManager entityMetadataManager,
34+
IParallelTableManager parallelTableManager,
35+
ILogger<ShardingBootstrapper> logger)
2936
{
3037
_logger = logger;
3138
_shardingProvider = shardingProvider;
32-
_routeConfigOptions = shardingProvider.GetRequiredService<IShardingRouteConfigOptions>();
33-
_entityMetadataManager = shardingProvider.GetRequiredService<IEntityMetadataManager>();
34-
_parallelTableManager = shardingProvider.GetRequiredService<IParallelTableManager>();
39+
_dbContextTypeAware = dbContextTypeAware;
40+
_routeConfigOptions = shardingRouteConfigOptions;
41+
_entityMetadataManager = entityMetadataManager;
42+
_parallelTableManager = parallelTableManager;
3543
}
3644
/// <summary>
3745
/// 初始化
@@ -63,6 +71,18 @@ private void InitializeEntityMetadata()
6371
var entityMetadataInitializer =(IEntityMetadataInitializer)_shardingProvider.CreateInstance(entityMetadataInitializerType);
6472
entityMetadataInitializer.Initialize();
6573
}
74+
75+
var allShardingEntities = _entityMetadataManager.GetAllShardingEntities();
76+
//判断如果有分表的对象那么dbcontext必须继承IShardingTableDbContext
77+
if (allShardingEntities.Any(o => _entityMetadataManager.IsShardingTable(o)))
78+
{
79+
var contextType = _dbContextTypeAware.GetContextType();
80+
if (!contextType.IsShardingTableDbContext())
81+
{
82+
throw new ShardingCoreInvalidOperationException(
83+
$"db context {contextType},has sharding table route,should be implement {nameof(IShardingTableDbContext)}");
84+
}
85+
}
6686
}
6787

6888
private void InitializeParallelTables()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace ShardingCore.Core.DbContextTypeAwares;
4+
5+
public class DbContextTypeAware:IDbContextTypeAware
6+
{
7+
private readonly Type _dbContextType;
8+
9+
public DbContextTypeAware(Type dbContextType)
10+
{
11+
_dbContextType = dbContextType;
12+
}
13+
public Type GetContextType()
14+
{
15+
return _dbContextType;
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace ShardingCore.Core.DbContextTypeAwares;
4+
5+
public interface IDbContextTypeAware
6+
{
7+
Type GetContextType();
8+
}

src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.Extensions.Logging;
44
using ShardingCore.Core.DbContextCreator;
5+
using ShardingCore.Core.DbContextTypeAwares;
56
using ShardingCore.Core.EntityMetadatas;
67
using ShardingCore.Core.QueryRouteManagers.Abstractions;
78
using ShardingCore.Core.QueryTrackers;
@@ -28,6 +29,7 @@ namespace ShardingCore.Core.RuntimeContexts
2829
public interface IShardingRuntimeContext
2930
{
3031
Type DbContextType { get; }
32+
IDbContextTypeAware GetDbContextTypeAware();
3133
IShardingProvider GetShardingProvider();
3234
ShardingConfigOptions GetShardingConfigOptions();
3335
IShardingRouteConfigOptions GetShardingRouteConfigOptions();

src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContextManager.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Logging;
55
using ShardingCore.Bootstrappers;
66
using ShardingCore.Core.DbContextCreator;
7+
using ShardingCore.Core.DbContextTypeAwares;
78
using ShardingCore.Core.EntityMetadatas;
89
using ShardingCore.Core.QueryRouteManagers.Abstractions;
910
using ShardingCore.Core.QueryTrackers;
@@ -72,9 +73,15 @@ public void AutoShardingCreate()
7273
{
7374
GetRequiredService<IShardingBootstrapper>().AutoShardingCreate();
7475
}
76+
private IDbContextTypeAware _dbContextTypeAware;
77+
public IDbContextTypeAware GetDbContextTypeAware()
78+
{
79+
return _dbContextTypeAware??=GetRequiredService<IDbContextTypeAware>();
80+
}
7581

7682
private IShardingProvider _shardingProvider;
7783

84+
7885
public IShardingProvider GetShardingProvider()
7986
{
8087
return _shardingProvider??=GetRequiredService<IShardingProvider>();
@@ -314,6 +321,7 @@ public IShardingRouteManager GetShardingRouteManager()
314321

315322
private void InitFieldValue()
316323
{
324+
GetDbContextTypeAware();
317325
GetShardingProvider();
318326
GetShardingConfigOptions();
319327
GetShardingRouteConfigOptions();

src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContextManager.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)