Skip to content

Commit

Permalink
remove GetNestedxxxBinder from QueryBinderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Dec 9, 2021
1 parent 8aa872f commit 43ada8b
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 89 deletions.
36 changes: 23 additions & 13 deletions src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9134,19 +9134,6 @@
Gets or sets the assembly resolver.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedFilterBinder">
<summary>
Gets or sets the nested filter binder.
For example: we do a orderby and a nested $filter.
$orderby=Addresses/$count($filter=HouseNumber gt 8) or
$filter=collectionProp/$count($filter=Name eq 'abc') gt 2
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedOrderByBinder">
<summary>
Gets or sets the nested orderby binder.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.ComputedProperties">
<summary>
Flattened list of properties from base query, for case when binder is applied for aggregated query.
Expand Down Expand Up @@ -9187,6 +9174,29 @@
an <see cref="T:System.Linq.Expressions.Expression"/>.
</summary>
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.#ctor(Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder,Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder)">
<summary>
Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder" /> class.
Select and Expand binder depends on <see cref="T:Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder"/> and <see cref="T:Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder"/> to process inner $filter and $orderby.
</summary>
<param name="filterBinder">The injected filter binder.</param>
<param name="orderByBinder">The injected orderby binder.</param>
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.#ctor">
<summary>
For unit test only.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.FilterBinder">
<summary>
Gets the filter binder.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.OrderByBinder">
<summary>
Gets the orderby binder.
</summary>
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.BindSelectExpand(Microsoft.OData.UriParser.SelectExpandClause,Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext)">
<summary>
Translate an OData $select or $expand tree represented by <see cref="T:Microsoft.OData.UriParser.SelectExpandClause"/> to an <see cref="T:System.Linq.Expressions.Expression"/>.
Expand Down
8 changes: 3 additions & 5 deletions src/Microsoft.AspNetCore.OData/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,6 @@ Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.ComputedProperti
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.CurrentParameter.get -> System.Linq.Expressions.ParameterExpression
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.ElementClrType.get -> System.Type
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.ElementType.get -> Microsoft.OData.Edm.IEdmType
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedFilterBinder.get -> System.Func<Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder>
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedFilterBinder.set -> void
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedOrderByBinder.get -> System.Func<Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder>
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetNestedOrderByBinder.set -> void
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.GetParameter(string name) -> System.Linq.Expressions.ParameterExpression
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.Model.get -> Microsoft.OData.Edm.IEdmModel
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.NavigationSource.get -> Microsoft.OData.Edm.IEdmNavigationSource
Expand All @@ -894,7 +890,9 @@ Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.QueryBinderConte
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.QuerySettings.get -> Microsoft.AspNetCore.OData.Query.ODataQuerySettings
Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext.RemoveParameter(string name) -> void
Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder
Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.SelectExpandBinder() -> void
Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.FilterBinder.get -> Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder
Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.OrderByBinder.get -> Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder
Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.SelectExpandBinder(Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder filterBinder, Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder orderByBinder) -> void
Microsoft.AspNetCore.OData.Query.FilterQueryOption
Microsoft.AspNetCore.OData.Query.FilterQueryOption.ApplyTo(System.Linq.IQueryable query, Microsoft.AspNetCore.OData.Query.ODataQuerySettings querySettings) -> System.Linq.IQueryable
Microsoft.AspNetCore.OData.Query.FilterQueryOption.Context.get -> Microsoft.AspNetCore.OData.Query.ODataQueryContext
Expand Down
35 changes: 18 additions & 17 deletions src/Microsoft.AspNetCore.OData/Query/Expressions/QueryBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,32 +509,33 @@ public virtual Expression BindCountNode(CountNode node, QueryBinderContext conte
countMethod = ExpressionHelperMethods.EnumerableCountGeneric.MakeGenericMethod(elementType);
}

MethodInfo whereMethod;
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
whereMethod = ExpressionHelperMethods.QueryableWhereGeneric.MakeGenericMethod(elementType);
}
else
{
whereMethod = ExpressionHelperMethods.EnumerableWhereGeneric.MakeGenericMethod(elementType);
}

// Bind the inner $filter clause within the $count segment.
// e.g Books?$filter=Authors/$count($filter=Id gt 1) gt 1
Expression filterExpression;
if (node.FilterClause != null)
{
// TODO: double check, sam xu
//QueryBinderContext subContext = context.CreateSubContext();
//subContext.ElementClrType = elementType;
IFilterBinder nestedFilterBinder = context.GetNestedFilterBinder();
QueryBinderContext nextBinderContext = new QueryBinderContext(context, context.QuerySettings, elementType);

filterExpression = nestedFilterBinder.BindFilter(node.FilterClause, nextBinderContext);
Expression body = Bind(node.FilterClause.Expression, nextBinderContext);

ParameterExpression filterParameter = nextBinderContext.CurrentParameter;

LambdaExpression filterExpr = Expression.Lambda(body, filterParameter);

filterExpr = Expression.Lambda(ApplyNullPropagationForFilterBody(filterExpr.Body, nextBinderContext), filterExpr.Parameters);

MethodInfo whereMethod;
if (typeof(IQueryable).IsAssignableFrom(source.Type))
{
whereMethod = ExpressionHelperMethods.QueryableWhereGeneric.MakeGenericMethod(elementType);
}
else
{
whereMethod = ExpressionHelperMethods.EnumerableWhereGeneric.MakeGenericMethod(elementType);
}

// The source expression looks like: $it.Authors
// So the generated countExpression below will look like: $it.Authors.Where($it => $it.Id > 1)
source = Expression.Call(null, whereMethod, new[] { source, filterExpression });
source = Expression.Call(null, whereMethod, new[] { source, filterExpr });
}

// append LongCount() method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.AspNetCore.OData.Edm;
using Microsoft.OData;
Expand Down Expand Up @@ -110,9 +109,6 @@ public QueryBinderContext(QueryBinderContext context, ODataQuerySettings querySe
ParameterExpression thisParameters = Expression.Parameter(clrType, DollarIt);
_lambdaParameters[DollarThis] = thisParameters;

GetNestedFilterBinder = context.GetNestedFilterBinder;
GetNestedOrderByBinder = context.GetNestedOrderByBinder;

IsNested = true;
}

Expand All @@ -136,19 +132,6 @@ public QueryBinderContext(QueryBinderContext context, ODataQuerySettings querySe
/// </summary>
public IAssemblyResolver AssembliesResolver { get; set; }

/// <summary>
/// Gets or sets the nested filter binder.
/// For example: we do a orderby and a nested $filter.
/// $orderby=Addresses/$count($filter=HouseNumber gt 8) or
/// $filter=collectionProp/$count($filter=Name eq 'abc') gt 2
/// </summary>
public Func<IFilterBinder> GetNestedFilterBinder { get; set; }

/// <summary>
/// Gets or sets the nested orderby binder.
/// </summary>
public Func<IOrderByBinder> GetNestedOrderByBinder { get; set; }

/// <summary>
/// Flattened list of properties from base query, for case when binder is applied for aggregated query.
/// Or the properties from $compute query options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ namespace Microsoft.AspNetCore.OData.Query.Expressions
/// </summary>
public class SelectExpandBinder : ISelectExpandBinder
{
/// <summary>
/// Initializes a new instance of the <see cref="SelectExpandBinder" /> class.
/// Select and Expand binder depends on <see cref="IFilterBinder"/> and <see cref="IOrderByBinder"/> to process inner $filter and $orderby.
/// </summary>
/// <param name="filterBinder">The injected filter binder.</param>
/// <param name="orderByBinder">The injected orderby binder.</param>
public SelectExpandBinder(IFilterBinder filterBinder, IOrderByBinder orderByBinder)
{
FilterBinder = filterBinder ?? throw Error.ArgumentNull(nameof(filterBinder));
OrderByBinder = orderByBinder ?? throw Error.ArgumentNull(nameof(orderByBinder));
}

/// <summary>
/// For unit test only.
/// </summary>
internal SelectExpandBinder()
: this(new FilterBinder(), new OrderByBinder())
{ }

/// <summary>
/// Gets the filter binder.
/// </summary>
public IFilterBinder FilterBinder { get; }

/// <summary>
/// Gets the orderby binder.
/// </summary>
public IOrderByBinder OrderByBinder { get; }

/// <summary>
/// Translate an OData $select or $expand tree represented by <see cref="SelectExpandClause"/> to an <see cref="Expression"/>.
/// </summary>
Expand Down Expand Up @@ -217,21 +246,20 @@ public virtual Expression CreatePropertyValueExpression(QueryBinderContext conte
newSettings.CopyFrom(context.QuerySettings);
newSettings.HandleNullPropagation = HandleNullPropagationOption.True;
QueryBinderContext binderContext = new QueryBinderContext(context, newSettings, clrElementType);
IFilterBinder filterBinder = binderContext.GetNestedFilterBinder();

if (isCollection)
{
Expression filterSource = nullablePropertyValue;

// TODO: Implement proper support for $select/$expand after $apply
// Expression filterPredicate = FilterBinder.Bind(null, filterClause, clrElementType, queryContext, querySettings);
filterResult = filterBinder.ApplyBind(filterSource, filterClause, binderContext);
filterResult = FilterBinder.ApplyBind(filterSource, filterClause, binderContext);

nullablePropertyType = filterResult.Type;
}
else if (settings.HandleReferenceNavigationPropertyExpandFilter)
{
LambdaExpression filterLambdaExpression = filterBinder.BindFilter(filterClause, binderContext) as LambdaExpression;
LambdaExpression filterLambdaExpression = FilterBinder.BindFilter(filterClause, binderContext) as LambdaExpression;
if (filterLambdaExpression == null)
{
throw new ODataException(Error.Format(SRResources.ExpandFilterExpressionNotLambdaExpression, edmProperty.Name, "LambdaExpression"));
Expand Down Expand Up @@ -943,7 +971,7 @@ private static SelectExpandClause GetOrCreateSelectExpandClause(IEdmNavigationPr
return new SelectExpandClause(selectItems, false);
}

private static Expression AddOrderByQueryForSource(QueryBinderContext context, Expression source, OrderByClause orderbyClause, Type elementType)
private Expression AddOrderByQueryForSource(QueryBinderContext context, Expression source, OrderByClause orderbyClause, Type elementType)
{
if (orderbyClause != null)
{
Expand All @@ -953,8 +981,7 @@ private static Expression AddOrderByQueryForSource(QueryBinderContext context, E
newSettings.HandleNullPropagation = HandleNullPropagationOption.True;
QueryBinderContext binderContext = new QueryBinderContext(context, newSettings, elementType);

IOrderByBinder binder = binderContext.GetNestedOrderByBinder();
source = binder.ApplyBind(source, orderbyClause, binderContext, false);
source = OrderByBinder.ApplyBind(source, orderbyClause, binderContext, false);
}

return source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static ISelectExpandBinder GetSelectExpandBinder(this ODataQueryContext c

ISelectExpandBinder binder = context.RequestContainer?.GetService<ISelectExpandBinder>();

return binder ?? new SelectExpandBinder();
return binder ?? new SelectExpandBinder(context.GetFilterBinder(), context.GetOrderByBinder());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ public IQueryable ApplyTo(IQueryable query, ODataQuerySettings querySettings)
var filterTransformation = transformation as FilterTransformationNode;

IFilterBinder binder = Context.GetFilterBinder();
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, ResultClrType)
{
GetNestedFilterBinder = () => binder
};
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, ResultClrType);

query = binder.ApplyBind(query, filterTransformation.FilterClause, binderContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ public IQueryable ApplyTo(IQueryable query, ODataQuerySettings querySettings)
Contract.Assert(filterClause != null);

IFilterBinder binder = Context.GetFilterBinder();
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, Context.ElementClrType)
{
GetNestedFilterBinder = () => binder
};
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, Context.ElementClrType);

return binder.ApplyBind(query, filterClause, binderContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ private IQueryable AddOrderByQueryForProperty(ODataQuerySettings querySettings,
// TODO: need to refactor it later.
orderbyClause = new OrderByClause(null, orderbyClause.Expression, orderbyClause.Direction, orderbyClause.RangeVariable);

QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, Context.ElementClrType)
{
GetNestedFilterBinder = () => Context.GetFilterBinder()
};
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, querySettings, Context.ElementClrType);

querySoFar = binder.ApplyBind(querySoFar, orderbyClause, binderContext, alreadyOrdered);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ public IQueryable ApplyTo(IQueryable queryable, ODataQuerySettings settings)
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, settings, Context.ElementClrType)
{
NavigationSource = Context.NavigationSource,
GetNestedFilterBinder = () => Context.GetFilterBinder(),
GetNestedOrderByBinder = () => Context.GetOrderByBinder()
};

return binder.ApplyBind(queryable, _selectExpandClause, binderContext);
Expand Down Expand Up @@ -246,8 +244,6 @@ public object ApplyTo(object entity, ODataQuerySettings settings)
QueryBinderContext binderContext = new QueryBinderContext(Context.Model, settings, Context.ElementClrType)
{
NavigationSource = Context.NavigationSource,
GetNestedFilterBinder = () => Context.GetFilterBinder(),
GetNestedOrderByBinder = () => Context.GetOrderByBinder()
};

return binder.ApplyBind(entity, _selectExpandClause, binderContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2685,8 +2685,6 @@ public class Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext {
System.Linq.Expressions.ParameterExpression CurrentParameter { public get; }
System.Type ElementClrType { public get; }
Microsoft.OData.Edm.IEdmType ElementType { public get; }
System.Func`1[[Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder]] GetNestedFilterBinder { public get; public set; }
System.Func`1[[Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder]] GetNestedOrderByBinder { public get; public set; }
Microsoft.OData.Edm.IEdmModel Model { public get; }
Microsoft.OData.Edm.IEdmNavigationSource NavigationSource { public get; public set; }
Microsoft.AspNetCore.OData.Query.ODataQuerySettings QuerySettings { public get; }
Expand All @@ -2696,7 +2694,10 @@ public class Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext {
}

public class Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder : ISelectExpandBinder {
public SelectExpandBinder ()
public SelectExpandBinder (Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder filterBinder, Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder orderByBinder)

Microsoft.AspNetCore.OData.Query.Expressions.IFilterBinder FilterBinder { public get; }
Microsoft.AspNetCore.OData.Query.Expressions.IOrderByBinder OrderByBinder { public get; }

public virtual System.Linq.Expressions.Expression BindSelectExpand (Microsoft.OData.UriParser.SelectExpandClause selectExpandClause, Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext context)
public virtual void BuildDynamicProperty (Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext context, System.Linq.Expressions.Expression source, Microsoft.OData.Edm.IEdmStructuredType structuredType, System.Collections.Generic.IList`1[[Microsoft.AspNetCore.OData.Query.Container.NamedPropertyExpression]] includedProperties)
Expand Down
Loading

0 comments on commit 43ada8b

Please sign in to comment.