IFilterInterceptor Of<T>

This commit is contained in:
David Lebee 2018-10-17 21:39:12 -05:00
parent 94d7b1c4d4
commit 3985530dbe
3 changed files with 25 additions and 10 deletions

View File

@ -7,7 +7,8 @@ namespace PoweredSoft.DynamicQuery.Cli
{ {
public class PersonQueryInterceptor : IQueryInterceptor public class PersonQueryInterceptor : IQueryInterceptor
//, IBeforeQueryAlteredInterceptor<Person> //, IBeforeQueryAlteredInterceptor<Person>
, IFilterInterceptor //, IFilterInterceptor
, IFilterInterceptor<Person>
{ {
public IQueryable<Person> InterceptQueryBeforeAltered(IQueryCriteria criteria, IQueryable<Person> queryable) public IQueryable<Person> InterceptQueryBeforeAltered(IQueryCriteria criteria, IQueryable<Person> queryable)
=> queryable.Where(t => t.FirstName.StartsWith("Da")); => queryable.Where(t => t.FirstName.StartsWith("Da"));
@ -38,6 +39,11 @@ namespace PoweredSoft.DynamicQuery.Cli
return filter; return filter;
} }
public IFilter InterceptFilter<T>(IFilter filter)
{
return InterceptFilter(filter);
}
} }
public class Person public class Person

View File

@ -6,4 +6,9 @@ namespace PoweredSoft.DynamicQuery.Core
{ {
IFilter InterceptFilter(IFilter filter); IFilter InterceptFilter(IFilter filter);
} }
public interface IFilterInterceptor<T> : IQueryInterceptor
{
IFilter InterceptFilter<T>(IFilter filter);
}
} }

View File

@ -60,38 +60,42 @@ namespace PoweredSoft.DynamicQuery
{ {
CurrentQueryable = CurrentQueryable.Query(whereBuilder => CurrentQueryable = CurrentQueryable.Query(whereBuilder =>
{ {
Criteria.Filters.ForEach(filter => ApplyFilter(whereBuilder, filter)); Criteria.Filters.ForEach(filter => ApplyFilter<T>(whereBuilder, filter));
}); });
} }
protected virtual void ApplyFilter(WhereBuilder whereBuilder, IFilter filter) protected virtual void ApplyFilter<T>(WhereBuilder whereBuilder, IFilter filter)
{ {
var transformedFilter = InterceptFilter(filter); var transformedFilter = InterceptFilter<T>(filter);
if (transformedFilter is ISimpleFilter) if (transformedFilter is ISimpleFilter)
ApplySimpleFilter(whereBuilder, transformedFilter as ISimpleFilter); ApplySimpleFilter<T>(whereBuilder, transformedFilter as ISimpleFilter);
else if (transformedFilter is ICompositeFilter) else if (transformedFilter is ICompositeFilter)
AppleCompositeFilter(whereBuilder, transformedFilter as ICompositeFilter); AppleCompositeFilter<T>(whereBuilder, transformedFilter as ICompositeFilter);
else else
throw new NotSupportedException(); throw new NotSupportedException();
} }
protected virtual void AppleCompositeFilter(WhereBuilder whereBuilder, ICompositeFilter filter) protected virtual void AppleCompositeFilter<T>(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<T>(subWhereBuilder, subFilter)), filter.And == true);
} }
protected virtual void ApplySimpleFilter(WhereBuilder whereBuilder, ISimpleFilter filter) protected virtual void ApplySimpleFilter<T>(WhereBuilder whereBuilder, ISimpleFilter filter)
{ {
var resolvedConditionOperator = ResolveFrom(filter.Type); var resolvedConditionOperator = ResolveFrom(filter.Type);
whereBuilder.Compare(filter.Path, resolvedConditionOperator, filter.Value, and: filter.And == true); whereBuilder.Compare(filter.Path, resolvedConditionOperator, filter.Value, and: filter.And == true);
} }
private IFilter InterceptFilter(IFilter filter) private IFilter InterceptFilter<T>(IFilter filter)
{ {
var ret = Interceptors.Where(t => t is IFilterInterceptor) var ret = Interceptors.Where(t => t is IFilterInterceptor)
.Cast<IFilterInterceptor>() .Cast<IFilterInterceptor>()
.Aggregate(filter, (previousFilter, interceptor) => interceptor.InterceptFilter(previousFilter)); .Aggregate(filter, (previousFilter, interceptor) => interceptor.InterceptFilter(previousFilter));
ret = Interceptors.Where(t => t is IFilterInterceptor<T>)
.Cast<IFilterInterceptor<T>>()
.Aggregate(filter, (previousFilter, interceptor) => interceptor.InterceptFilter<T>(previousFilter));
return ret; return ret;
} }