Skip to content

Commit 0f06a7a

Browse files
committed
Merge branch 'main' of github.com:dotnetcore/sharding-core
2 parents 25a8c2e + 9246b09 commit 0f06a7a

File tree

22 files changed

+185
-181
lines changed

22 files changed

+185
-181
lines changed

samples/Sample.MySql/Controllers/WeatherForecastController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public IQueryable<SysTest> GetAll()
6565
// //一定要先在路由里面添加尾巴
6666
// virtualTableRoute.Append("20220921");
6767
// shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
68-
6968
return _defaultTableDbContext.Set<SysTest>();
7069
}
7170
[HttpGet]

samples/Sample.MySql/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
179179
{
180180
app.UseDeveloperExceptionPage();
181181
}
182-
app.ApplicationServices.UseAutoShardingCreate();
183182
// app.ApplicationServices.UseAutoTryCompensateTable();
184183

185184
// app.ApplicationServices.UseAutoShardingCreate();

samples/Sample.ShardingConsole/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using ShardingCore;
66
using ShardingCore.Extensions;
77

8-
ShardingProvider.ShardingRuntimeContext.UseAutoShardingCreate();
98
ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable();
109

1110
var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

src/ShardingCore/Bootstrappers/IShardingBootstrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ShardingCore.Bootstrappers
1010
/// 主要的分片初始化器,需要手动调用,如果你的分片路由存在定时执行的job譬如
1111
/// 系统默认的时间分片的情况下那么需要调用<code>IShardingRuntimeContext初始化的时候会调用</code>
1212
/// </summary>
13-
public interface IShardingBootstrapper
13+
internal interface IShardingBootstrapper
1414
{
1515
void AutoShardingCreate();
1616
}

src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ namespace ShardingCore.Bootstrappers
2525
* @Date: Monday, 21 December 2020 09:10:07
2626
* @Email: 326308290@qq.com
2727
*/
28-
public class ShardingBootstrapper : IShardingBootstrapper
28+
internal class ShardingBootstrapper : IShardingBootstrapper
2929
{
3030
private readonly IShardingProvider _shardingProvider;
31-
private readonly IDbContextCreator _dbContextCreator;
3231
private readonly DoOnlyOnce _onlyOnce=new DoOnlyOnce();
33-
public ShardingBootstrapper(IShardingProvider shardingProvider,IDbContextCreator dbContextCreator)
32+
public ShardingBootstrapper(IShardingProvider shardingProvider)
3433
{
3534
_shardingProvider = shardingProvider;
36-
_dbContextCreator = dbContextCreator;
3735
}
3836
public void AutoShardingCreate()
3937
{
4038
if (!_onlyOnce.IsUnDo())
4139
return;
42-
CheckRequirement();
4340
StartAutoShardingJob();
4441
}
4542

@@ -51,29 +48,6 @@ private void StartAutoShardingJob()
5148
await jobRunnerService.StartAsync();
5249
}, TaskCreationOptions.LongRunning);
5350
}
54-
private void CheckRequirement()
55-
{
56-
try
57-
{
58-
using (var scope = _shardingProvider.CreateScope())
59-
{
60-
using (var dbContext = _dbContextCreator.GetShellDbContext(scope.ServiceProvider))
61-
{
62-
if (dbContext == null)
63-
{
64-
throw new ShardingCoreInvalidOperationException(
65-
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}");
66-
}
67-
}
68-
}
69-
}
70-
catch (Exception ex)
71-
{
72-
throw new ShardingCoreInvalidOperationException(
73-
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
74-
ex);
75-
}
76-
}
7751

7852
}
7953
}

src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.EntityFrameworkCore;
77
using ShardingCore.Core.ServiceProviders;
8+
using ShardingCore.Exceptions;
89
using ShardingCore.Helpers;
910
using ShardingCore.Sharding.Abstractions;
1011

@@ -46,7 +47,16 @@ public virtual DbContext CreateDbContext(DbContext shellDbContext, ShardingDbCon
4647

4748
public virtual DbContext GetShellDbContext(IShardingProvider shardingProvider)
4849
{
49-
return shardingProvider.GetService<TShardingDbContext>();
50+
try
51+
{
52+
return shardingProvider.GetService<TShardingDbContext>();
53+
}
54+
catch (Exception ex)
55+
{
56+
throw new ShardingCoreInvalidOperationException(
57+
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
58+
ex);
59+
}
5060
}
5161
}
5262
}

src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,8 @@ public interface IShardingRuntimeContext
5656
IShardingPageManager GetShardingPageManager();
5757
IDataSourceInitializer GetDataSourceInitializer();
5858

59-
void CheckRequirement();
60-
6159
void GetOrCreateShardingRuntimeModel(DbContext dbContext);
6260
void Initialize();
63-
void AutoShardingCreate();
6461
object GetService(Type serviceType);
6562
TService GetService<TService>();
6663
object GetRequiredService(Type serviceType);

src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using ShardingCore.Exceptions;
2424

2525
using ShardingCore.Sharding.Abstractions;
26+
using ShardingCore.Sharding.MergeEngines.ParallelControl;
2627
using ShardingCore.Sharding.ParallelTables;
2728
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
2829
using ShardingCore.Sharding.ShardingComparision.Abstractions;
@@ -67,10 +68,11 @@ public void Initialize()
6768
_serviceProvider = _serviceMap.BuildServiceProvider();
6869
_serviceProvider.GetRequiredService<IShardingInitializer>().Initialize();
6970
InitFieldValue();
71+
AutoShardingCreate();
7072
}
7173
}
7274

73-
public void AutoShardingCreate()
75+
private void AutoShardingCreate()
7476
{
7577
GetRequiredService<IShardingBootstrapper>().AutoShardingCreate();
7678
}
@@ -217,40 +219,6 @@ public IDataSourceInitializer GetDataSourceInitializer()
217219
return _dataSourceInitializer??=GetRequiredService<IDataSourceInitializer>();
218220
}
219221

220-
public void CheckRequirement()
221-
{
222-
if (isCheckRequirement)
223-
return;
224-
225-
lock (CHECK_REQUIREMENT)
226-
{
227-
if (isCheckRequirement)
228-
return;
229-
isCheckRequirement = true;
230-
231-
try
232-
{
233-
var shardingProvider = GetShardingProvider();
234-
using (var scope = shardingProvider.CreateScope())
235-
{
236-
using (var dbContext = _dbContextCreator.GetShellDbContext(scope.ServiceProvider))
237-
{
238-
if (dbContext == null)
239-
{
240-
throw new ShardingCoreInvalidOperationException(
241-
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}");
242-
}
243-
}
244-
}
245-
}
246-
catch (Exception ex)
247-
{
248-
throw new ShardingCoreInvalidOperationException(
249-
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
250-
ex);
251-
}
252-
}
253-
}
254222

255223
public void GetOrCreateShardingRuntimeModel(DbContext dbContext)
256224
{

src/ShardingCore/EFCores/RelationTransactions/ShardingRelationalTransaction.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,19 @@ public override void Rollback()
9292
await _shardingDbContext.CommitAsync(cancellationToken);
9393
_shardingDbContext.NotifyShardingTransaction();
9494
}
95-
// #if !NETCOREAPP3_0
96-
// public override void CreateSavepoint(string name)
97-
// {
98-
// AAA
99-
// base.CreateSavepoint(name);
100-
// }
101-
// #endif
95+
#if !NETCOREAPP3_0&&!NETSTANDARD2_0
96+
// public override void CreateSavepoint(string name)
97+
// {
98+
// base.CreateSavepoint(name);
99+
// _shardingDbContext.CreateSavepoint(name);
100+
// }
101+
//
102+
// public override async Task CreateSavepointAsync(string name, CancellationToken cancellationToken = new CancellationToken())
103+
// {
104+
// await base.CreateSavepointAsync(name, cancellationToken);
105+
// await _shardingDbContext.CreateSavepointAsync(name,cancellationToken);
106+
// }
107+
#endif
102108
#endif
103109
}
104110
}

src/ShardingCore/Extensions/ShardingRuntimeExtension.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
@@ -10,19 +11,13 @@ namespace ShardingCore.Extensions
1011

1112
public static class ShardingRuntimeExtension
1213
{
13-
public static void UseAutoShardingCreate(this IShardingRuntimeContext shardingRuntimeContext)
14-
{
15-
shardingRuntimeContext.CheckRequirement();
16-
shardingRuntimeContext.AutoShardingCreate();
17-
}
1814
/// <summary>
1915
/// 自动尝试补偿表
2016
/// </summary>
2117
/// <param name="shardingRuntimeContext"></param>
2218
/// <param name="parallelCount"></param>
2319
public static void UseAutoTryCompensateTable(this IShardingRuntimeContext shardingRuntimeContext, int? parallelCount = null)
2420
{
25-
shardingRuntimeContext.CheckRequirement();
2621
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
2722
var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer();
2823
var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions();

0 commit comments

Comments
 (0)