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:
parent
13f4ef0b83
commit
e217896243
@ -9,6 +9,7 @@ namespace PoweredSoft.DynamicQuery.Cli
|
||||
{
|
||||
public class PersonQueryInterceptor : IQueryInterceptor
|
||||
, IAggregateInterceptor
|
||||
, IQueryConvertInterceptor<Person>
|
||||
//, IBeforeQueryAlteredInterceptor<Person>
|
||||
//, IFilterInterceptor
|
||||
{
|
||||
@ -44,10 +45,21 @@ namespace PoweredSoft.DynamicQuery.Cli
|
||||
|
||||
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 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
|
||||
@ -56,13 +68,18 @@ namespace PoweredSoft.DynamicQuery.Cli
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public int Age { get; set; }
|
||||
public string Sexe { get; set; }
|
||||
public string AgeStr => $"{Age} years old";
|
||||
public string Sex { get; set; }
|
||||
}
|
||||
|
||||
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
|
||||
@ -78,11 +95,11 @@ namespace PoweredSoft.DynamicQuery.Cli
|
||||
{
|
||||
var list = new List<Person>()
|
||||
{
|
||||
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 = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20},
|
||||
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30},
|
||||
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sexe = "Female", Age = 30},
|
||||
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sex = "Male", Age = 29 },
|
||||
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sex = "Female", Age = 29},
|
||||
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sex = "Female", Age = 20},
|
||||
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sex = "Male", Age = 30},
|
||||
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sex = "Female", Age = 30},
|
||||
};
|
||||
|
||||
var queryable = list.AsQueryable();
|
||||
|
12
PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs
Normal file
12
PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs
Normal 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);
|
||||
}
|
||||
}
|
@ -6,6 +6,4 @@ namespace PoweredSoft.DynamicQuery.Core
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -69,9 +69,15 @@ namespace PoweredSoft.DynamicQuery
|
||||
cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}");
|
||||
|
||||
if (!isLast)
|
||||
{
|
||||
cgrr.Data = new List<object>();
|
||||
}
|
||||
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())
|
||||
previousGroupResults.Last().Data.Add(cgrr);
|
||||
@ -204,9 +210,13 @@ namespace PoweredSoft.DynamicQuery
|
||||
// sorts and paging.
|
||||
ApplySorting<T>();
|
||||
ApplyPaging<T>();
|
||||
|
||||
// the data.
|
||||
result.Data = CurrentQueryable.ToObjectList();
|
||||
|
||||
// data.
|
||||
var entities = ((IQueryable<T>)CurrentQueryable).ToList();
|
||||
var records = InterceptConvertTo<T>(entities);
|
||||
result.Data = records;
|
||||
|
||||
// aggregates.
|
||||
result.Aggregates = CalculateTotalAggregate<T>(afterFilterQueryable);
|
||||
|
||||
return result;
|
||||
|
@ -96,6 +96,36 @@ namespace PoweredSoft.DynamicQuery
|
||||
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)
|
||||
{
|
||||
var original = new List<ISort>()
|
||||
|
Loading…
Reference in New Issue
Block a user