added feature for batch readings alterations.

This commit is contained in:
David Lebee
2019-02-23 22:47:50 -06:00
parent 9b51e2e287
commit 61642eee18
6 changed files with 183 additions and 13 deletions
+7 -4
View File
@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using PoweredSoft.DynamicLinq;
using PoweredSoft.DynamicLinq.Fluent;
using PoweredSoft.DynamicQuery.Core;
@@ -57,13 +58,15 @@ namespace PoweredSoft.DynamicQuery
var groupRecords = CurrentQueryable.ToDynamicClassList();
// now join them into logical collections
result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First());
var lastLists = new List<List<object>>();
result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First(), lastLists);
// intercept grouped by.
QueryInterceptToGrouped<T>(lastLists).Wait();
result.Aggregates = CalculateTotalAggregate<T>(queryableAfterFilters);
return result;
}
protected virtual List<IAggregateResult> CalculateTotalAggregate<T>(IQueryable queryableAfterFilters)
{
if (!Criteria.Aggregates.Any())
@@ -107,7 +110,7 @@ namespace PoweredSoft.DynamicQuery
// data.
var entities = ((IQueryable<T>)CurrentQueryable).ToList();
var records = InterceptConvertTo<T>(entities);
var records = InterceptConvertTo<T>(entities).Result;
result.Data = records;
// aggregates.
@@ -70,7 +70,11 @@ namespace PoweredSoft.DynamicQuery
var groupRecords = await AsyncQueryableService.ToListAsync(CurrentQueryable.Cast<DynamicClass>(), cancellationToken);
// now join them into logical collections
result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First());
var lastLists = new List<List<object>>();
result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First(), lastLists);
// converted to grouped by.
await QueryInterceptToGrouped<T>(lastLists);
result.Aggregates = await CalculateTotalAggregateAsync<T>(queryableAfterFilters, cancellationToken);
return result;
@@ -93,7 +97,7 @@ namespace PoweredSoft.DynamicQuery
// data.
var entities = await AsyncQueryableService.ToListAsync(((IQueryable<T>)CurrentQueryable), cancellationToken);
var records = InterceptConvertTo<T>(entities);
var records = await InterceptConvertTo<T>(entities);
result.Data = records;
// aggregates.
+57 -4
View File
@@ -182,15 +182,46 @@ namespace PoweredSoft.DynamicQuery
return ret;
}
protected virtual List<object> InterceptConvertTo<T>(List<T> entities)
protected virtual async Task<List<object>> InterceptConvertTo<T>(List<T> entities)
{
await AfterEntityReadInterceptors(entities);
var objects = entities.Cast<object>().ToList();
for (var i = 0; i < objects.Count; i++)
objects[i] = InterceptConvertToObject<T>(objects[i]);
var pairs = entities.Select((t, index) => Tuple.Create(t, objects[index])).ToList();
await AfterReadInterceptors<T>(pairs);
return objects;
}
protected virtual async Task AfterEntityReadInterceptors<T>(List<T> entities)
{
Interceptors
.Where(t => t is IAfterReadEntityInterceptor<T>)
.Cast<IAfterReadEntityInterceptor<T>>()
.ToList()
.ForEach(t => t.AfterReadEntity(entities));
var asyncInterceptors = Interceptors.Where(t => t is IAfterReadEntityInterceptorAsync<T>).Cast<IAfterReadEntityInterceptorAsync<T>>();
foreach (var interceptor in asyncInterceptors)
await interceptor.AfterReadEntityAsync(entities);
}
protected virtual async Task AfterReadInterceptors<T>(List<Tuple<T, object>> pairs)
{
Interceptors
.Where(t => t is IAfterReadInterceptor<T>)
.Cast<IAfterReadInterceptor<T>>()
.ToList()
.ForEach(t => t.AfterRead(pairs));
var asyncInterceptors = Interceptors.Where(t => t is IAfterReadInterceptorAsync<T>).Cast<IAfterReadInterceptorAsync<T>>();
foreach (var interceptor in asyncInterceptors)
await interceptor.AfterReadAsync(pairs);
}
protected virtual object InterceptConvertToObject<T>(object o)
{
o = Interceptors
@@ -329,7 +360,7 @@ namespace PoweredSoft.DynamicQuery
.Aggregate((IQueryable<T>)CurrentQueryable, (prev, interceptor) => interceptor.InterceptBeforeFiltering(Criteria, prev));
}
protected virtual List<object> RecursiveRegroup<T>(List<DynamicClass> groupRecords, List<List<DynamicClass>> aggregateResults, IGroup group, List<IGroupQueryResult> parentGroupResults = null)
protected virtual List<object> RecursiveRegroup<T>(List<DynamicClass> groupRecords, List<List<DynamicClass>> aggregateResults, IGroup group, List<List<object>> lastLists, List<IGroupQueryResult> parentGroupResults = null)
{
var groupIndex = Criteria.Groups.IndexOf(group);
var isLast = Criteria.Groups.Last() == group;
@@ -376,11 +407,12 @@ namespace PoweredSoft.DynamicQuery
if (isLast)
{
var entities = t.SelectMany(t2 => t2.GetDynamicPropertyValue<List<T>>("Records")).ToList();
groupResult.Data = InterceptConvertTo<T>(entities);
groupResult.Data = entities.Cast<object>().ToList();
lastLists.Add(groupResult.Data);
}
else
{
groupResult.Data = RecursiveRegroup<T>(t.ToList(), aggregateResults, Criteria.Groups[groupIndex + 1], groupResults);
groupResult.Data = RecursiveRegroup<T>(t.ToList(), aggregateResults, Criteria.Groups[groupIndex + 1], lastLists, groupResults);
}
return groupResult;
@@ -389,5 +421,26 @@ namespace PoweredSoft.DynamicQuery
.ToList();
return ret;
}
protected virtual async Task QueryInterceptToGrouped<T>(List<List<object>> lists)
{
var entities = lists.SelectMany(t => t).Cast<T>().ToList();
await AfterEntityReadInterceptors(entities);
var pairs = new List<Tuple<T, object>>();
lists.ForEach(innerList =>
{
for(var i = 0; i < innerList.Count; i++)
{
var entity = (T)innerList[i];
var convertedObject = InterceptConvertToObject<T>(entity);
innerList[i] = convertedObject;
pairs.Add(Tuple.Create(entity, convertedObject));
}
});
await AfterReadInterceptors<T>(pairs);
}
}
}