From 3985530dbe19709e828c431ba14e26e3cee99a16 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Wed, 17 Oct 2018 21:39:12 -0500 Subject: [PATCH] IFilterInterceptor Of --- PoweredSoft.DynamicQuery.Cli/Program.cs | 8 ++++++- .../IFilterInterceptor.cs | 5 +++++ PoweredSoft.DynamicQuery/QueryHandler.cs | 22 +++++++++++-------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/PoweredSoft.DynamicQuery.Cli/Program.cs b/PoweredSoft.DynamicQuery.Cli/Program.cs index 20749a2..affa8ab 100644 --- a/PoweredSoft.DynamicQuery.Cli/Program.cs +++ b/PoweredSoft.DynamicQuery.Cli/Program.cs @@ -7,7 +7,8 @@ namespace PoweredSoft.DynamicQuery.Cli { public class PersonQueryInterceptor : IQueryInterceptor //, IBeforeQueryAlteredInterceptor - , IFilterInterceptor + //, IFilterInterceptor + , IFilterInterceptor { public IQueryable InterceptQueryBeforeAltered(IQueryCriteria criteria, IQueryable queryable) => queryable.Where(t => t.FirstName.StartsWith("Da")); @@ -38,6 +39,11 @@ namespace PoweredSoft.DynamicQuery.Cli return filter; } + + public IFilter InterceptFilter(IFilter filter) + { + return InterceptFilter(filter); + } } public class Person diff --git a/PoweredSoft.DynamicQuery.Core/IFilterInterceptor.cs b/PoweredSoft.DynamicQuery.Core/IFilterInterceptor.cs index 1d72c90..1585d6c 100644 --- a/PoweredSoft.DynamicQuery.Core/IFilterInterceptor.cs +++ b/PoweredSoft.DynamicQuery.Core/IFilterInterceptor.cs @@ -6,4 +6,9 @@ namespace PoweredSoft.DynamicQuery.Core { IFilter InterceptFilter(IFilter filter); } + + public interface IFilterInterceptor : IQueryInterceptor + { + IFilter InterceptFilter(IFilter filter); + } } diff --git a/PoweredSoft.DynamicQuery/QueryHandler.cs b/PoweredSoft.DynamicQuery/QueryHandler.cs index 0a5fdbf..89fb80d 100644 --- a/PoweredSoft.DynamicQuery/QueryHandler.cs +++ b/PoweredSoft.DynamicQuery/QueryHandler.cs @@ -60,38 +60,42 @@ namespace PoweredSoft.DynamicQuery { CurrentQueryable = CurrentQueryable.Query(whereBuilder => { - Criteria.Filters.ForEach(filter => ApplyFilter(whereBuilder, filter)); + Criteria.Filters.ForEach(filter => ApplyFilter(whereBuilder, filter)); }); } - protected virtual void ApplyFilter(WhereBuilder whereBuilder, IFilter filter) + protected virtual void ApplyFilter(WhereBuilder whereBuilder, IFilter filter) { - var transformedFilter = InterceptFilter(filter); + var transformedFilter = InterceptFilter(filter); if (transformedFilter is ISimpleFilter) - ApplySimpleFilter(whereBuilder, transformedFilter as ISimpleFilter); + ApplySimpleFilter(whereBuilder, transformedFilter as ISimpleFilter); else if (transformedFilter is ICompositeFilter) - AppleCompositeFilter(whereBuilder, transformedFilter as ICompositeFilter); + AppleCompositeFilter(whereBuilder, transformedFilter as ICompositeFilter); else throw new NotSupportedException(); } - protected virtual void AppleCompositeFilter(WhereBuilder whereBuilder, ICompositeFilter filter) + protected virtual void AppleCompositeFilter(WhereBuilder whereBuilder, ICompositeFilter filter) { - whereBuilder.SubQuery(subWhereBuilder => filter.Filters.ForEach(subFilter => ApplyFilter(subWhereBuilder, subFilter)), filter.And == true); + whereBuilder.SubQuery(subWhereBuilder => filter.Filters.ForEach(subFilter => ApplyFilter(subWhereBuilder, subFilter)), filter.And == true); } - protected virtual void ApplySimpleFilter(WhereBuilder whereBuilder, ISimpleFilter filter) + protected virtual void ApplySimpleFilter(WhereBuilder whereBuilder, ISimpleFilter filter) { var resolvedConditionOperator = ResolveFrom(filter.Type); whereBuilder.Compare(filter.Path, resolvedConditionOperator, filter.Value, and: filter.And == true); } - private IFilter InterceptFilter(IFilter filter) + private IFilter InterceptFilter(IFilter filter) { var ret = Interceptors.Where(t => t is IFilterInterceptor) .Cast() .Aggregate(filter, (previousFilter, interceptor) => interceptor.InterceptFilter(previousFilter)); + ret = Interceptors.Where(t => t is IFilterInterceptor) + .Cast>() + .Aggregate(filter, (previousFilter, interceptor) => interceptor.InterceptFilter(previousFilter)); + return ret; }