Skip to content

Commit 97d2a76

Browse files
committed
优化批量操作的不分库操作,完成表达式批量操作
1 parent 8e315dd commit 97d2a76

File tree

7 files changed

+64
-49
lines changed

7 files changed

+64
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ AbstractSimpleShardingYearKeyLongVirtualTableRoute |按时间戳 |yyyy | `>,>=,<
537537
:`contains`表示为`o=>ids.contains(o.shardingkey)`
538538
:使用默认的按时间分表的路由规则会让你重写一个GetBeginTime的方法这个方法必须使用静态值如:new DateTime(2021,1,1)不可以用动态值比如DateTime.Now因为每次重新启动都会调用该方法动态情况下会导致每次都不一致
539539

540-
#高级
540+
# 高级
541541

542542
## 批量操作
543543

nuget-publish.bat

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
:start
22
::定义版本
3-
set EFCORE3=3.2.0.20
4-
set EFCORE5=5.2.0.20
3+
set EFCORE2=2.3.0.00-pre1
4+
set EFCORE3=3.3.0.00-pre1
5+
set EFCORE5=5.3.0.00-pre1
56

67
::删除所有bin与obj下的文件
78
@echo off

src/ShardingCore/Extensions/ShardingExtension.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,74 @@ public BulkDicEntry(DbContext innerDbContext, LinkedList<TEntity> innerEntities)
212212
public DbContext InnerDbContext { get; }
213213
public LinkedList<TEntity> InnerEntities { get; }
214214
}
215+
216+
public static Dictionary<DbContext, IEnumerable<TEntity>> BulkShardingTableEnumerable<TShardingDbContext, TEntity>(this TShardingDbContext shardingDbContext,
217+
IEnumerable<TEntity> entities) where TShardingDbContext : DbContext, IShardingDbContext
218+
where TEntity : class
219+
{
220+
221+
if (typeof(TEntity).IsShardingDataSource())
222+
throw new InvalidOperationException(typeof(TEntity).FullName);
223+
return shardingDbContext.BulkShardingEnumerable(entities).First().Value;
224+
}
215225
/// <summary>
216226
/// 根据条件表达式解析
217227
/// </summary>
228+
/// <typeparam name="TShardingDbContext"></typeparam>
218229
/// <typeparam name="TEntity"></typeparam>
219230
/// <param name="shardingDbContext"></param>
220231
/// <param name="where"></param>
221232
/// <returns></returns>
222-
public static IEnumerable<DbContext> BulkShardingExpression<TEntity>(this IShardingDbContext shardingDbContext, Expression<Func<TEntity, bool>> where) where TEntity : class
233+
public static IDictionary<string, IEnumerable<DbContext>> BulkShardingExpression<TShardingDbContext, TEntity>(this TShardingDbContext shardingDbContext, Expression<Func<TEntity, bool>> where) where TEntity : class
234+
where TShardingDbContext : DbContext, IShardingDbContext
235+
{
236+
237+
var virtualDataSource = ShardingContainer.GetService<IVirtualDataSource<TShardingDbContext>>();
238+
var routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
239+
var virtualTableManager = ShardingContainer.GetService<IVirtualTableManager<TShardingDbContext>>();
240+
241+
var dataSourceNames = virtualDataSource.GetDataSourceNames(where);
242+
var result = new Dictionary<string, LinkedList<DbContext>>();
243+
var entityType = typeof(TEntity);
244+
245+
foreach (var dataSourceName in dataSourceNames)
246+
{
247+
if (!result.TryGetValue(dataSourceName, out var dbContexts))
248+
{
249+
dbContexts = new LinkedList<DbContext>();
250+
result.Add(dataSourceName, dbContexts);
251+
}
252+
if (entityType.IsShardingTable())
253+
{
254+
var physicTables = virtualTableManager.GetVirtualTable(entityType).RouteTo(new ShardingTableRouteConfig(predicate:@where));
255+
if(physicTables.IsEmpty())
256+
throw new ShardingCoreException($"{where.ShardingPrint()} cant found ant physic table");
257+
258+
var dbs = physicTables.Select(o => shardingDbContext.GetDbContext(dataSourceName, true, routeTailFactory.Create(o.Tail))).ToList();
259+
foreach (var dbContext in dbs)
260+
{
261+
dbContexts.AddLast(dbContext);
262+
}
263+
}
264+
else
265+
{
266+
var dbContext = shardingDbContext.GetDbContext(dataSourceName, true, routeTailFactory.Create(string.Empty));
267+
dbContexts.AddLast(dbContext);
268+
}
269+
270+
}
271+
272+
return result.ToDictionary(o=>o.Key,o=>(IEnumerable<DbContext>)o.Value);
273+
}
274+
275+
public static IEnumerable<DbContext> BulkShardingTableExpression<TShardingDbContext, TEntity>(this TShardingDbContext shardingDbContext, Expression<Func<TEntity, bool>> where) where TEntity : class
276+
where TShardingDbContext : DbContext, IShardingDbContext
223277
{
224-
return shardingDbContext.CreateExpressionDbContext(where);
278+
279+
if (typeof(TEntity).IsShardingDataSource())
280+
throw new InvalidOperationException(typeof(TEntity).FullName);
281+
return shardingDbContext.BulkShardingExpression<TShardingDbContext, TEntity>(where).First().Value;
225282
}
283+
226284
}
227285
}

src/ShardingCore/Sharding/AbstractShardingDbContext.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ public DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity :
8686
return _shardingDbContextExecutor.CreateGenericDbContext(entity);
8787
}
8888

89-
public IEnumerable<DbContext> CreateExpressionDbContext<TEntity>(Expression<Func<TEntity, bool>> where) where TEntity : class
90-
{
91-
return _shardingDbContextExecutor.CreateExpressionDbContext(where);
92-
}
93-
9489

9590

9691
public override EntityEntry Add(object entity)

src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ public interface IShardingDbContext
3535
/// <param name="entity"></param>
3636
/// <returns></returns>
3737
DbContext CreateGenericDbContext<T>(T entity) where T : class;
38-
/// <summary>
39-
/// 根据表达式创建db context
40-
/// </summary>
41-
/// <typeparam name="TEntity"></typeparam>
42-
/// <param name="where"></param>
43-
/// <returns></returns>
44-
45-
IEnumerable<DbContext> CreateExpressionDbContext<TEntity>(Expression<Func<TEntity, bool>> where)
46-
where TEntity : class;
4738

4839

4940

src/ShardingCore/Sharding/Abstractions/IShardingDbContextExecutor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public interface IShardingDbContextExecutor : IDisposable
4747
DbContext CreateDbContext(bool parallelQuery, string dataSourceName, IRouteTail routeTail);
4848

4949
DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity : class;
50-
IEnumerable<DbContext> CreateExpressionDbContext<TEntity>(Expression<Func<TEntity, bool>> where)
51-
where TEntity : class;
5250

5351
IShardingTransaction BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.Unspecified);
5452

src/ShardingCore/Sharding/ShardingDbContextExecutor.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,6 @@ public DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity :
159159
return CreateDbContext(true, dataSourceName, _routeTailFactory.Create(tail));
160160
}
161161

162-
163-
public IEnumerable<DbContext> CreateExpressionDbContext<TEntity>(Expression<Func<TEntity, bool>> @where) where TEntity : class
164-
{
165-
166-
var dataSourceNames = _virtualDataSource.GetDataSourceNames(where);
167-
168-
if (typeof(TEntity).IsShardingTable())
169-
{
170-
var resultDbContexts = new LinkedList<DbContext>();
171-
foreach (var dataSourceName in dataSourceNames)
172-
{
173-
var physicTables = _virtualTableManager.GetVirtualTable(typeof(TEntity)).RouteTo(new ShardingTableRouteConfig(predicate: where));
174-
if (physicTables.IsEmpty())
175-
throw new ShardingCoreException($"{where.ShardingPrint()} cant found ant physic table");
176-
var dbContexts = physicTables.Select(o => CreateDbContext(true, dataSourceName, _routeTailFactory.Create(o.Tail))).ToList();
177-
foreach (var dbContext in dbContexts)
178-
{
179-
resultDbContexts.AddLast(dbContext);
180-
}
181-
}
182-
183-
return resultDbContexts;
184-
}
185-
else
186-
{
187-
return dataSourceNames.Select(dataSourceName => CreateDbContext(true, dataSourceName, _routeTailFactory.Create(string.Empty)));
188-
}
189-
}
190162
#endregion
191163

192164
#region transaction

0 commit comments

Comments
 (0)