next would be to add a bit more interceptors, example to interface the result of an aggregate and change it :)

This commit is contained in:
David Lebee 2018-10-21 20:27:50 -05:00
parent 13f4ef0b83
commit e217896243
5 changed files with 83 additions and 16 deletions

View File

@ -9,6 +9,7 @@ namespace PoweredSoft.DynamicQuery.Cli
{ {
public class PersonQueryInterceptor : IQueryInterceptor public class PersonQueryInterceptor : IQueryInterceptor
, IAggregateInterceptor , IAggregateInterceptor
, IQueryConvertInterceptor<Person>
//, IBeforeQueryAlteredInterceptor<Person> //, IBeforeQueryAlteredInterceptor<Person>
//, IFilterInterceptor //, IFilterInterceptor
{ {
@ -44,10 +45,21 @@ namespace PoweredSoft.DynamicQuery.Cli
public IAggregate InterceptAggregate(IAggregate aggregate) public IAggregate InterceptAggregate(IAggregate aggregate)
{ {
if (aggregate.Path == nameof(Person.AgeStr)) if (aggregate.Path == nameof(PersonModel.AgeStr))
return new Aggregate {Type = aggregate.Type, Path = nameof(Person.Age)}; return new Aggregate {Type = aggregate.Type, Path = nameof(Person.Age)};
return aggregate; return aggregate;
} }
public object InterceptResultTo(Person entity)
{
var personModel = new PersonModel();
personModel.Id = entity.Id;
personModel.FirstName = entity.FirstName;
personModel.LastName = entity.LastName;
personModel.Age = entity.Age;
personModel.Sex = entity.Sex;
return personModel;
}
} }
public class Person public class Person
@ -56,13 +68,18 @@ namespace PoweredSoft.DynamicQuery.Cli
public string FirstName { get; set; } public string FirstName { get; set; }
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 Sex { get; set; }
public string AgeStr => $"{Age} years old";
} }
public class OtherClass public class PersonModel
{ {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
public string AgeStr => $"{Age} years old";
public string FullName => $"{FirstName} {LastName}";
} }
class Program class Program
@ -78,11 +95,11 @@ 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", Sex = "Male", Age = 29 },
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sexe = "Female", Age = 29}, new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sex = "Female", Age = 29},
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20}, new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sex = "Female", Age = 20},
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30}, new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sex = "Male", Age = 30},
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sexe = "Female", Age = 30}, new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sex = "Female", Age = 30},
}; };
var queryable = list.AsQueryable(); var queryable = list.AsQueryable();

View File

@ -0,0 +1,12 @@
namespace PoweredSoft.DynamicQuery.Core
{
public interface IQueryConvertInterceptor : IQueryInterceptor
{
object InterceptResultTo(object entity);
}
public interface IQueryConvertInterceptor<T> : IQueryInterceptor
{
object InterceptResultTo(T entity);
}
}

View File

@ -6,6 +6,4 @@ namespace PoweredSoft.DynamicQuery.Core
{ {
} }
} }

View File

@ -69,9 +69,15 @@ namespace PoweredSoft.DynamicQuery
cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}"); cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}");
if (!isLast) if (!isLast)
{
cgrr.Data = new List<object>(); cgrr.Data = new List<object>();
}
else else
cgrr.Data = groupRecord.GetDynamicPropertyValue<List<T>>("Records").Cast<object>().ToList(); {
var entities = groupRecord.GetDynamicPropertyValue<List<T>>("Records");
var records = InterceptConvertTo<T>(entities);
cgrr.Data = records;
}
if (previousGroupResults.Any()) if (previousGroupResults.Any())
previousGroupResults.Last().Data.Add(cgrr); previousGroupResults.Last().Data.Add(cgrr);
@ -205,8 +211,12 @@ namespace PoweredSoft.DynamicQuery
ApplySorting<T>(); ApplySorting<T>();
ApplyPaging<T>(); ApplyPaging<T>();
// the data. // data.
result.Data = CurrentQueryable.ToObjectList(); var entities = ((IQueryable<T>)CurrentQueryable).ToList();
var records = InterceptConvertTo<T>(entities);
result.Data = records;
// aggregates.
result.Aggregates = CalculateTotalAggregate<T>(afterFilterQueryable); result.Aggregates = CalculateTotalAggregate<T>(afterFilterQueryable);
return result; return result;

View File

@ -96,6 +96,36 @@ namespace PoweredSoft.DynamicQuery
return ret; return ret;
} }
protected virtual List<object> InterceptConvertTo<T>(List<T> entities)
{
var objects = entities.Cast<object>().ToList();
for (var i = 0; i < objects.Count; i++)
objects[i] = InterceptConvertToObject<T>(objects[i]);
return objects;
}
protected virtual object InterceptConvertToObject<T>(object o)
{
o = Interceptors
.Where(t => t is IQueryConvertInterceptor)
.Cast<IQueryConvertInterceptor>()
.Aggregate(o, (prev, interceptor) => interceptor.InterceptResultTo(prev));
o = Interceptors
.Where(t => t is IQueryConvertInterceptor<T>)
.Cast<IQueryConvertInterceptor<T>>()
.Aggregate(o, (prev, interceptor) =>
{
if (prev is T)
return interceptor.InterceptResultTo((T)prev);
return o;
});
return o;
}
protected virtual List<ISort> InterceptSort<T>(ISort sort) protected virtual List<ISort> InterceptSort<T>(ISort sort)
{ {
var original = new List<ISort>() var original = new List<ISort>()