dotnet-dynamic-query/PoweredSoft.DynamicQuery/QueryHandlerAsync.cs

143 lines
6.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
2019-03-22 16:27:04 -04:00
using PoweredSoft.Data;
using PoweredSoft.Data.Core;
using PoweredSoft.DynamicLinq;
using PoweredSoft.DynamicQuery.Core;
namespace PoweredSoft.DynamicQuery
{
2019-03-22 16:27:04 -04:00
public class QueryHandlerAsync : QueryHandlerBase, IQueryHandlerAsync
{
public IAsyncQueryableService AsyncQueryableService { get; }
public QueryHandlerAsync(IAsyncQueryableService asyncQueryableService)
{
AsyncQueryableService = asyncQueryableService;
}
2019-03-22 16:27:04 -04:00
protected virtual Task<IQueryExecutionResult<TRecord>> FinalExecuteAsync<TSource, TRecord>(CancellationToken cancellationToken = default(CancellationToken))
{
2019-03-22 16:27:04 -04:00
CommonBeforeExecute<TSource>();
return HasGrouping ? ExecuteAsyncGrouping<TSource, TRecord>(cancellationToken) : ExecuteAsyncNoGrouping<TSource, TRecord>(cancellationToken);
}
2019-03-22 16:27:04 -04:00
protected virtual async Task<IQueryExecutionResult<TRecord>> ExecuteAsyncGrouping<TSource, TRecord>(CancellationToken cancellationToken)
{
2019-03-22 16:27:04 -04:00
var result = new QueryExecutionGroupResult<TRecord>();
// preserve queryable.
var queryableAfterFilters = CurrentQueryable;
// async.
2019-03-22 16:27:04 -04:00
result.TotalRecords = await this.AsyncQueryableService.LongCountAsync((IQueryable<TSource>)queryableAfterFilters, cancellationToken);
CalculatePageCount(result);
// intercept groups in advance to avoid doing it more than once :)
2019-03-22 16:27:04 -04:00
var finalGroups = Criteria.Groups.Select(g => InterceptGroup<TSource>(g)).ToList();
// get the aggregates.
2019-03-22 16:27:04 -04:00
var aggregateResults = await FetchAggregatesAsync<TSource>(finalGroups, cancellationToken);
// sorting.
finalGroups.ReversedForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending)));
// apply sorting and paging.
2019-03-22 16:27:04 -04:00
ApplySorting<TSource>();
ApplyPaging<TSource>();
// create group & select expression.
CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}")));
CurrentQueryable = CurrentQueryable.Select(sb =>
{
finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}"));
sb.ToList("Records");
});
// loop through the grouped records.
var groupRecords = await AsyncQueryableService.ToListAsync(CurrentQueryable.Cast<DynamicClass>(), cancellationToken);
// now join them into logical collections
2019-03-22 16:27:04 -04:00
var lastLists = new List<(List<TSource> entities, IGroupQueryResult<TRecord> group)>();
result.Groups = RecursiveRegroup<TSource, TRecord>(groupRecords, aggregateResults, Criteria.Groups.First(), lastLists);
// converted to grouped by.
2019-03-22 16:27:04 -04:00
await QueryInterceptToGrouped<TSource, TRecord>(lastLists);
2019-03-22 16:27:04 -04:00
result.Aggregates = await CalculateTotalAggregateAsync<TSource>(queryableAfterFilters, cancellationToken);
return result;
}
2019-03-22 16:27:04 -04:00
protected async Task<IQueryExecutionResult<TRecord>> ExecuteAsyncNoGrouping<TSource, TRecord>(CancellationToken cancellationToken)
{
2019-03-22 16:27:04 -04:00
var result = new QueryExecutionResult<TRecord>();
// after filter queryable
2019-03-22 16:27:04 -04:00
IQueryable<TSource> afterFilterQueryable = (IQueryable<TSource>)CurrentQueryable;
// total records.
result.TotalRecords = await AsyncQueryableService.LongCountAsync(afterFilterQueryable, cancellationToken);
CalculatePageCount(result);
// sorts and paging.
2019-03-22 16:27:04 -04:00
ApplySorting<TSource>();
ApplyPaging<TSource>();
// data.
2019-03-22 16:27:04 -04:00
var entities = await AsyncQueryableService.ToListAsync(((IQueryable<TSource>)CurrentQueryable), cancellationToken);
var records = await InterceptConvertTo<TSource, TRecord>(entities);
result.Data = records;
// aggregates.
2019-03-22 16:27:04 -04:00
result.Aggregates = await CalculateTotalAggregateAsync<TSource>(afterFilterQueryable, cancellationToken);
return result;
}
2019-03-22 16:27:04 -04:00
protected virtual async Task<List<IAggregateResult>> CalculateTotalAggregateAsync<TSource>(IQueryable queryableAfterFilters, CancellationToken cancellationToken)
{
if (!Criteria.Aggregates.Any())
return null;
2019-03-22 16:27:04 -04:00
IQueryable selectExpression = CreateTotalAggregateSelectExpression<TSource>(queryableAfterFilters);
var aggregateResult = await AsyncQueryableService.FirstOrDefaultAsync(selectExpression.Cast<DynamicClass>());
return MaterializeCalculateTotalAggregateResult(aggregateResult);
}
2019-03-22 16:27:04 -04:00
protected async virtual Task<List<List<DynamicClass>>> FetchAggregatesAsync<TSource>(List<IGroup> finalGroups, CancellationToken cancellationToken)
{
if (!Criteria.Aggregates.Any())
return null;
var previousGroups = new List<IGroup>();
var whenAllResult = await Task.WhenAll(finalGroups.Select(fg =>
{
2019-03-22 16:27:04 -04:00
IQueryable selectExpression = CreateFetchAggregateSelectExpression<TSource>(fg, previousGroups);
var selectExpressionCasted = selectExpression.Cast<DynamicClass>();
var aggregateResult = AsyncQueryableService.ToListAsync(selectExpressionCasted, cancellationToken);
previousGroups.Add(fg);
return aggregateResult;
}));
var finalResult = whenAllResult.ToList();
return finalResult;
}
2019-03-22 16:27:04 -04:00
public Task<IQueryExecutionResult<TSource>> ExecuteAsync<TSource>(IQueryable<TSource> queryable, IQueryCriteria criteria, CancellationToken cancellationToken = default(CancellationToken))
{
Reset(queryable, criteria);
return FinalExecuteAsync<TSource, TSource>(cancellationToken);
}
public Task<IQueryExecutionResult<TRecord>> ExecuteAsync<TSource, TRecord>(IQueryable<TSource> queryable, IQueryCriteria criteria, CancellationToken cancellationToken = default(CancellationToken))
{
Reset(queryable, criteria);
return FinalExecuteAsync<TSource, TRecord>(cancellationToken);
}
}
}