intercept aggregate
This commit is contained in:
parent
c0fa4c91b1
commit
13f4ef0b83
@ -8,6 +8,7 @@ using Newtonsoft.Json.Converters;
|
|||||||
namespace PoweredSoft.DynamicQuery.Cli
|
namespace PoweredSoft.DynamicQuery.Cli
|
||||||
{
|
{
|
||||||
public class PersonQueryInterceptor : IQueryInterceptor
|
public class PersonQueryInterceptor : IQueryInterceptor
|
||||||
|
, IAggregateInterceptor
|
||||||
//, IBeforeQueryAlteredInterceptor<Person>
|
//, IBeforeQueryAlteredInterceptor<Person>
|
||||||
//, IFilterInterceptor
|
//, IFilterInterceptor
|
||||||
{
|
{
|
||||||
@ -40,6 +41,13 @@ namespace PoweredSoft.DynamicQuery.Cli
|
|||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IAggregate InterceptAggregate(IAggregate aggregate)
|
||||||
|
{
|
||||||
|
if (aggregate.Path == nameof(Person.AgeStr))
|
||||||
|
return new Aggregate {Type = aggregate.Type, Path = nameof(Person.Age)};
|
||||||
|
return aggregate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Person
|
public class Person
|
||||||
@ -49,6 +57,7 @@ namespace PoweredSoft.DynamicQuery.Cli
|
|||||||
public string LastName { get; set; }
|
public string LastName { get; set; }
|
||||||
public int Age { get; set; }
|
public int Age { get; set; }
|
||||||
public string Sexe { get; set; }
|
public string Sexe { get; set; }
|
||||||
|
public string AgeStr => $"{Age} years old";
|
||||||
}
|
}
|
||||||
|
|
||||||
public class OtherClass
|
public class OtherClass
|
||||||
@ -69,7 +78,7 @@ namespace PoweredSoft.DynamicQuery.Cli
|
|||||||
{
|
{
|
||||||
var list = new List<Person>()
|
var list = new List<Person>()
|
||||||
{
|
{
|
||||||
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sexe = "Male", Age = 29},
|
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sexe = "Male", Age = 29 },
|
||||||
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sexe = "Female", Age = 29},
|
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sexe = "Female", Age = 29},
|
||||||
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20},
|
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20},
|
||||||
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30},
|
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30},
|
||||||
@ -90,7 +99,7 @@ namespace PoweredSoft.DynamicQuery.Cli
|
|||||||
criteria.Aggregates = new List<IAggregate>()
|
criteria.Aggregates = new List<IAggregate>()
|
||||||
{
|
{
|
||||||
new Aggregate { Type = AggregateType.Count },
|
new Aggregate { Type = AggregateType.Count },
|
||||||
new Aggregate { Path = "Age", Type = AggregateType.Avg }
|
new Aggregate { Path = "AgeStr", Type = AggregateType.Avg }
|
||||||
};;
|
};;
|
||||||
|
|
||||||
var handler = new QueryHandler();
|
var handler = new QueryHandler();
|
||||||
|
11
PoweredSoft.DynamicQuery.Core/IAggregateInterceptor.cs
Normal file
11
PoweredSoft.DynamicQuery.Core/IAggregateInterceptor.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicQuery.Core
|
||||||
|
{
|
||||||
|
public interface IAggregateInterceptor : IQueryInterceptor
|
||||||
|
{
|
||||||
|
IAggregate InterceptAggregate(IAggregate aggregate);
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
|
var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
|
||||||
|
|
||||||
// get the aggregates.
|
// get the aggregates.
|
||||||
var aggregateResults = Criteria.Aggregates.Any() ? FetchAggregates(finalGroups) : null;
|
var aggregateResults = FetchAggregates<T>(finalGroups);
|
||||||
|
|
||||||
// sorting.
|
// sorting.
|
||||||
finalGroups.ForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending)));
|
finalGroups.ForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending)));
|
||||||
@ -101,11 +101,11 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
return (object)groupRecordResult;
|
return (object)groupRecordResult;
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
result.Aggregates = CalculateTotalAggregate(queryableAfterFilters);
|
result.Aggregates = CalculateTotalAggregate<T>(queryableAfterFilters);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual List<IAggregateResult> CalculateTotalAggregate(IQueryable queryableAfterFilters)
|
protected virtual List<IAggregateResult> CalculateTotalAggregate<T>(IQueryable queryableAfterFilters)
|
||||||
{
|
{
|
||||||
if (!Criteria.Aggregates.Any())
|
if (!Criteria.Aggregates.Any())
|
||||||
return null;
|
return null;
|
||||||
@ -115,8 +115,9 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
{
|
{
|
||||||
Criteria.Aggregates.ForEach((a, index) =>
|
Criteria.Aggregates.ForEach((a, index) =>
|
||||||
{
|
{
|
||||||
var selectType = ResolveSelectFrom(a.Type);
|
var fa = InterceptAggregate<T>(a);
|
||||||
sb.Aggregate(a.Path, selectType, $"Agg_{index}");
|
var selectType = ResolveSelectFrom(fa.Type);
|
||||||
|
sb.Aggregate(fa.Path, selectType, $"Agg_{index}");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -153,8 +154,11 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private List<List<DynamicClass>> FetchAggregates(List<IGroup> finalGroups)
|
private List<List<DynamicClass>> FetchAggregates<T>(List<IGroup> finalGroups)
|
||||||
{
|
{
|
||||||
|
if (!Criteria.Aggregates.Any())
|
||||||
|
return null;
|
||||||
|
|
||||||
List<List<DynamicClass>> aggregateResults;
|
List<List<DynamicClass>> aggregateResults;
|
||||||
var previousGroups = new List<IGroup>();
|
var previousGroups = new List<IGroup>();
|
||||||
aggregateResults = finalGroups.Select(fg =>
|
aggregateResults = finalGroups.Select(fg =>
|
||||||
@ -173,8 +177,9 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}");
|
sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}");
|
||||||
Criteria.Aggregates.ForEach((a, ai) =>
|
Criteria.Aggregates.ForEach((a, ai) =>
|
||||||
{
|
{
|
||||||
var selectType = ResolveSelectFrom(a.Type);
|
var fa = InterceptAggregate<T>(a);
|
||||||
sb.Aggregate(a.Path, selectType, $"Agg_{ai}");
|
var selectType = ResolveSelectFrom(fa.Type);
|
||||||
|
sb.Aggregate(fa.Path, selectType, $"Agg_{ai}");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -202,7 +207,7 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
|
|
||||||
// the data.
|
// the data.
|
||||||
result.Data = CurrentQueryable.ToObjectList();
|
result.Data = CurrentQueryable.ToObjectList();
|
||||||
result.Aggregates = CalculateTotalAggregate(afterFilterQueryable);
|
result.Aggregates = CalculateTotalAggregate<T>(afterFilterQueryable);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ using System.Diagnostics;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using PoweredSoft.DynamicLinq.Fluent;
|
using PoweredSoft.DynamicLinq.Fluent;
|
||||||
@ -86,6 +87,14 @@ namespace PoweredSoft.DynamicQuery
|
|||||||
result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0);
|
result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual IAggregate InterceptAggregate<T>(IAggregate aggregate)
|
||||||
|
{
|
||||||
|
var ret = Interceptors
|
||||||
|
.Where(t => t is IAggregateInterceptor)
|
||||||
|
.Cast<IAggregateInterceptor>()
|
||||||
|
.Aggregate(aggregate, (prev, inter) => inter.InterceptAggregate(prev));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual List<ISort> InterceptSort<T>(ISort sort)
|
protected virtual List<ISort> InterceptSort<T>(ISort sort)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user