How to take OData Queryable Web API endpoint filter and map it from DTO object?

跟風遠走 提交于 2021-01-04 03:07:12

问题


I have a simple Web API endpoint which can accept incoming OData queries:

public IActionResult GetProducts(ODataQueryOptions<ProductDTO> options)
{
    var results = DomainLayer.GetProducts(options);
    return Ok(results);
}

I specifically want to be able to query against ProductDTO objects and to be able to filter or sort against the properties of the DTO representation.

My design issue is that I want to take advantage of the filter parsing/applying logic of the OData library but I don't want to expose my database-bound ProductEntity objects to my Web API AND I do not want to return an IQueryable from my DataAccessLayer, only IEnumerables.

What I am trying to do then is to extract the Expression from the FilterQueryOption property of the incoming ODataQueryOptions so I can use AutoMapper's Expression Mapping feature to map the expression from a Expression<Func<ProductDTO, bool>> to a Expression<Func<Product, bool>> then finally to a Expression<Func<ProductEntity, bool>> where I will then pass it into a .Where() call on my Table<ProductEntity> where (hopefully) the filter is applied in my SQL database (via Linq-2-SQL) and then I just convert it all the way back to a DTO object after.

The big showstopper I came across is that queryable.Expression is returning a MethodCallExpression rather than a Expression<Func<ProductDTO, bool>> like I expected, which means I can't map the expression with AutoMapper like I had planned...

How can I get around this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.AspNet.OData.Query;
using AutoMapper.Extensions.ExpressionMapping;
using AutoMapper.QueryableExtensions;

namespace ProductApp
{
    public class DomainLayer
    {
        public IEnumerable<ProductDTO> GetProductsByEntityOptions(ODataQueryOptions<ProductDTO> options)
        {
            var mapper = MyMapper.GetMapper();

            // This is the trick to get the expression out of the FilterQueryOption...
            IQueryable queryable = Enumerable.Empty<ProductDTO>().AsQueryable();
            queryable = options.Filter.ApplyTo(queryable, new ODataQuerySettings());            
            var exp = (MethodCallExpression) queryable.Expression;              // <-- This comes back as a MethodCallExpression...

            // Map the expression to my intermediate Product object type
            var mappedExp = mapper.Map<Expression<Func<Product, bool>>>(exp);   // <-- But I want it as a Expression<Func<ProductDTO, bool>> so I can map it...

            IEnumerable<Product> results = _dataAccessLayer.GetProducts(mappedExp);

            return mapper.Map<IEnumerable<ProductDTO>>(results);
        }
    }

    public class DataAccessLayer
    {
        public IEnumerable<Product> GetProducts(Expression<Func<Product, bool>> exp)
        {
            var mapper = MyMapper.GetMapper();

            var mappedExp = mapper.Map<Expression<Func<ProductEntity, bool>>>(exp);
            IEnumerable<ProductEntity> result = _dataContext.GetTable<ProductEntity>().Where(mappedExpression).ToList();

            return mapper.Map<IEnumerable<Product>>(result);
        }
    }
}

References:

  • Where I found the trick to get the Expression out of the filter: https://stackoverflow.com/a/16447514/1504964
  • A related GitHub Issue: https://github.com/OData/WebApi/issues/33

回答1:


Well, the author of the accepted answer of the linked post wrote at the end:

Notice that the expression contains looks more like this, SOTests.Customer[].Where($it => conditional-expression). So, you might have to extract that conditional expression from the lambda.

The MethodCallExpression you are getting is exactly that - a "call" to Queryable.Where<ProductDTO>, and the lambda expression Expression<Func<ProductDTO, bool>> you need is the second argument (remember the Queryable.Where is a static extension method, so the first argument is representing the IQueryable<ProductDTO>), wrapped with Expression.Quote.

So all you need is to extract the lambda expression with something like this:

public static class ODataQueryOptionsExtensions
{
    public static Expression<Func<T, bool>> GetFilter<T>(this ODataQueryOptions<T> options)
    {
        // The same trick as in the linked post
        IQueryable query = Enumerable.Empty<T>().AsQueryable();
        query = options.Filter.ApplyTo(query, new ODataQuerySettings());
        // Extract the predicate from `Queryable.Where` call
        var call = query.Expression as MethodCallExpression;
        if (call != null && call.Method.Name == nameof(Queryable.Where) && call.Method.DeclaringType == typeof(Queryable))
        {
            var predicate = ((UnaryExpression)call.Arguments[1]).Operand;
            return (Expression<Func<T, bool>>)predicate;
        }
        return null;
    }
}

and use it like this:

public class DomainLayer
{
    public IEnumerable<ProductDTO> GetProductsByEntityOptions(ODataQueryOptions<ProductDTO> options)
    {
         var filter = options.GetFilter();
         // Here the type of filter variable is Expression<Func<ProductDTO, bool>> as desired
         // The rest ...
    }
}


来源:https://stackoverflow.com/questions/55307370/how-to-take-odata-queryable-web-api-endpoint-filter-and-map-it-from-dto-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!