Skip to content

Commit 8a998f7

Browse files
committed
添加对changetracker的支持 发布x.6.0.24
1 parent 9593b70 commit 8a998f7

File tree

17 files changed

+360
-77
lines changed

17 files changed

+360
-77
lines changed

samples/Sample.Migrations/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Sample.Migrations.EFCores;
1515
using ShardingCore;
1616
using ShardingCore.Bootstrappers;
17+
using ShardingCore.Core.RuntimeContexts;
1718
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
1819

1920
namespace Sample.Migrations
@@ -30,7 +31,6 @@ public Startup(IConfiguration configuration)
3031
// This method gets called by the runtime. Use this method to add services to the container.
3132
public void ConfigureServices(IServiceCollection services)
3233
{
33-
3434
services.AddControllers();
3535

3636
//services.AddDbContext<DefaultShardingTableDbContext>((sp, builder) =>

samples/Sample.MySql/Controllers/WeatherForecastController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public async Task<IActionResult> Get()
4646
// Console.WriteLine("------------");
4747
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
4848
// {
49-
50-
var resultX = await _defaultTableDbContext.Set<SysUserMod>()
49+
var sysUserMods = _defaultTableDbContext.Set<SysUserMod>();
50+
var resultX = await _defaultTableDbContext.Set<SysUserMod>()
5151
.Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
5252
var resultY = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
5353
var shardingFirstOrDefaultAsyncxxx = await _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o=>o.Time==DateTime.Now).ToListAsync();

samples/Sample.MySql/Startup.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void ConfigureServices(IServiceCollection services)
9191
});
9292
o.AddDefaultDataSource("ds0",
9393
"server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;");
94-
}).AddShardingCore();
94+
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore();
9595
services.AddSingleton<IShardingRuntimeContext>(sp =>
9696
{
9797
Stopwatch stopwatch = Stopwatch.StartNew();
@@ -196,21 +196,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
196196
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
197197
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
198198
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
199-
// using (var scope = app.ApplicationServices.CreateScope())
200-
// {
201-
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
202-
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
203-
// {
204-
// try
205-
// {
206-
//
207-
// defaultShardingDbContext.Database.Migrate();
208-
// }
209-
// catch (Exception e)
210-
// {
211-
// }
212-
// }
213-
// }
199+
using (var scope = app.ApplicationServices.CreateScope())
200+
{
201+
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
202+
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
203+
{
204+
try
205+
{
206+
207+
defaultShardingDbContext.Database.Migrate();
208+
}
209+
catch (Exception e)
210+
{
211+
}
212+
}
213+
}
214214
// using (var scope = app.ApplicationServices.CreateScope())
215215
// {
216216
// var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();

samples/Sample.SqlServer/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using System;
1111
using System.Collections.Generic;
1212
using System.Diagnostics;
13+
using ShardingCore.Core.RuntimeContexts;
14+
using ShardingCore.Extensions;
1315
using ShardingCore.Helpers;
1416
using ShardingCore.Sharding.ReadWriteConfigurations;
1517

@@ -32,7 +34,6 @@ public void ConfigureServices(IServiceCollection services)
3234
{
3335
services.AddControllers();
3436
//services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True"));
35-
3637
services.AddShardingDbContext<DefaultShardingDbContext>()
3738
.UseRouteConfig(o =>
3839
{

src/ShardingCore/Bootstrappers/IShardingInitializer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace ShardingCore.Bootstrappers
22
{
3-
3+
/// <summary>
4+
/// 分片初始化器主要用来初始化元数据信息和平行表
5+
/// </summary>
46
internal interface IShardingInitializer
57
{
6-
78
/// <summary>
89
/// 初始化
910
/// </summary>

src/ShardingCore/Core/DbContextCreator/ShardingDbContextOptions.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55

66
namespace ShardingCore.Core.DbContextCreator
77
{
8-
/*
9-
* @Author: xjm
10-
* @Description:
11-
* @Date: Wednesday, 16 December 2020 16:15:43
12-
* @Email: 326308290@qq.com
13-
*/
8+
/// <summary>
9+
/// 用来实现dbcontext的创建,将RouteTail和DbContextOptions封装到一起
10+
/// </summary>
1411
public class ShardingDbContextOptions
1512
{
1613
public ShardingDbContextOptions(DbContextOptions dbContextOptions, IRouteTail routeTail)
@@ -19,7 +16,13 @@ public ShardingDbContextOptions(DbContextOptions dbContextOptions, IRouteTail ro
1916
DbContextOptions = dbContextOptions;
2017
}
2118

19+
/// <summary>
20+
/// 用来告诉ShardingCore创建的DbContext是什么后缀
21+
/// </summary>
2222
public IRouteTail RouteTail{ get; }
23+
/// <summary>
24+
/// 用来创建DbContext
25+
/// </summary>
2326
public DbContextOptions DbContextOptions { get; }
2427
}
2528
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ShardingCore.Core.Internal
6+
{
7+
8+
public class MultiEnumerator<TEntity>:IEnumerator<TEntity>
9+
{
10+
private readonly List<IEnumerator<TEntity>> _enumerators;
11+
private int index;
12+
private readonly int _enumeratorsCount;
13+
14+
public MultiEnumerator(IEnumerable<IEnumerator<TEntity>> enumerators)
15+
{
16+
_enumerators = enumerators.ToList();
17+
_enumeratorsCount = _enumerators.Count;
18+
index = 0;
19+
}
20+
public bool MoveNext()
21+
{
22+
if (_enumeratorsCount == 0)
23+
{
24+
return false;
25+
}
26+
27+
if (index >= _enumeratorsCount)
28+
{
29+
return false;
30+
}
31+
32+
while (index < _enumeratorsCount)
33+
{
34+
var moveNext = _enumerators[index].MoveNext();
35+
if (moveNext)
36+
{
37+
return true;
38+
}
39+
index++;
40+
}
41+
return false;
42+
}
43+
44+
public void Reset()
45+
{
46+
throw new System.NotImplementedException();
47+
}
48+
49+
public TEntity Current => _enumerators[index].Current;
50+
51+
object IEnumerator.Current => Current;
52+
53+
public void Dispose()
54+
{
55+
_enumerators.Clear();
56+
}
57+
}
58+
}

src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace ShardingCore.Core.ShardingConfigurations
99
{
10+
/// <summary>
11+
/// 分片配置
12+
/// </summary>
1013
public class ShardingConfigOptions
1114
{
1215
/// <summary>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.ChangeTracking;
6+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
7+
using Microsoft.EntityFrameworkCore.Metadata;
8+
using ShardingCore.Sharding.Abstractions;
9+
10+
namespace ShardingCore.EFCores.ChangeTrackers
11+
{
12+
public class ShardingChangeTracker : ChangeTracker
13+
{
14+
private readonly DbContext _dbContext;
15+
16+
public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector,
17+
IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model,
18+
graphIterator)
19+
{
20+
_dbContext = context;
21+
}
22+
#if !EFCORE2 && !EFCORE3 && !EFCORE5 && !EFCORE6
23+
1
24+
#endif
25+
26+
public override bool HasChanges()
27+
{
28+
if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover)
29+
{
30+
return currentDbContextDiscover.GetCurrentDbContexts().Any(o =>
31+
o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges()));
32+
}
33+
34+
return base.HasChanges();
35+
}
36+
37+
public override IEnumerable<EntityEntry> Entries()
38+
{
39+
if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover)
40+
{
41+
return currentDbContextDiscover.GetCurrentDbContexts().SelectMany(o =>
42+
o.Value.GetCurrentContexts().SelectMany(cd => cd.Value.ChangeTracker.Entries()));
43+
}
44+
45+
return base.Entries();
46+
}
47+
48+
public override IEnumerable<EntityEntry<TEntity>> Entries<TEntity>()
49+
{
50+
if (_dbContext is ICurrentDbContextDiscover currentDbContextDiscover)
51+
{
52+
return currentDbContextDiscover.GetCurrentDbContexts().SelectMany(o =>
53+
o.Value.GetCurrentContexts().SelectMany(cd => cd.Value.ChangeTracker.Entries<TEntity>()));
54+
}
55+
56+
return base.Entries<TEntity>();
57+
}
58+
59+
public override void DetectChanges()
60+
{
61+
if (_dbContext is ICurrentDbContextDiscover)
62+
{
63+
Do(c => c.DetectChanges());
64+
return;
65+
}
66+
base.DetectChanges();
67+
}
68+
69+
public override void AcceptAllChanges()
70+
{
71+
if (_dbContext is ICurrentDbContextDiscover)
72+
{
73+
Do(c => c.AcceptAllChanges());
74+
return;
75+
}
76+
base.AcceptAllChanges();
77+
}
78+
79+
private void Do(Action<ChangeTracker> action)
80+
{
81+
var dataSourceDbContexts = ((ICurrentDbContextDiscover)_dbContext).GetCurrentDbContexts();
82+
foreach (var dataSourceDbContext in dataSourceDbContexts)
83+
{
84+
var currentContexts = dataSourceDbContext.Value.GetCurrentContexts();
85+
foreach (var keyValuePair in currentContexts)
86+
{
87+
action(keyValuePair.Value.ChangeTracker);
88+
}
89+
}
90+
}
91+
92+
public override void TrackGraph(object rootEntity, Action<EntityEntryGraphNode> callback)
93+
{
94+
if (_dbContext is ICurrentDbContextDiscover)
95+
{
96+
Do(c => c.TrackGraph(rootEntity,callback));
97+
return;
98+
}
99+
base.TrackGraph(rootEntity, callback);
100+
}
101+
102+
#if !EFCORE2
103+
public override void TrackGraph<TState>(object rootEntity, TState state, Func<EntityEntryGraphNode<TState>, bool> callback) where TState : default
104+
{
105+
if (_dbContext is ICurrentDbContextDiscover)
106+
{
107+
Do(c => c.TrackGraph(rootEntity,state,callback));
108+
return;
109+
}
110+
base.TrackGraph(rootEntity, state, callback);
111+
}
112+
113+
public override void CascadeChanges()
114+
{
115+
if (_dbContext is ICurrentDbContextDiscover)
116+
{
117+
Do(c => c.CascadeChanges());
118+
return;
119+
}
120+
base.CascadeChanges();
121+
}
122+
123+
#endif
124+
#if !EFCORE2 && !EFCORE3
125+
public override void Clear()
126+
{
127+
if (_dbContext is ICurrentDbContextDiscover)
128+
{
129+
Do(c => c.Clear());
130+
return;
131+
}
132+
base.Clear();
133+
}
134+
#endif
135+
136+
#if EFCORE2
137+
public override void TrackGraph<TState>(object rootEntity, TState state, Func<EntityEntryGraphNode, TState, bool> callback)
138+
{
139+
if (_dbContext is ICurrentDbContextDiscover)
140+
{
141+
Do(c => c.TrackGraph(rootEntity,state,callback));
142+
return;
143+
}
144+
base.TrackGraph(rootEntity, state, callback);
145+
}
146+
#endif
147+
}
148+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.EntityFrameworkCore.ChangeTracking;
2+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Internal;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
7+
namespace ShardingCore.EFCores.ChangeTrackers
8+
{
9+
10+
public class ShardingChangeTrackerFactory:ChangeTrackerFactory
11+
{
12+
private readonly ICurrentDbContext _currentContext;
13+
private readonly IStateManager _stateManager;
14+
private readonly IChangeDetector _changeDetector;
15+
private readonly IModel _model;
16+
private readonly IEntityEntryGraphIterator _graphIterator;
17+
18+
public ShardingChangeTrackerFactory(ICurrentDbContext currentContext, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(currentContext, stateManager, changeDetector, model, graphIterator)
19+
{
20+
_currentContext = currentContext;
21+
_stateManager = stateManager;
22+
_changeDetector = changeDetector;
23+
_model = model;
24+
_graphIterator = graphIterator;
25+
}
26+
public ChangeTracker Create()
27+
{
28+
return new ShardingChangeTracker(_currentContext.Context, _stateManager, _changeDetector, _model,
29+
_graphIterator);
30+
}
31+
32+
}
33+
}

0 commit comments

Comments
 (0)