ObjectSet包装器不使用linqToEntities子查询

时间:2014-02-06 12:31:55

标签: c# linq entity-framework-4 wrapper iqueryable

为了在密集数据库使用系统中进行访问控制,我必须实现一个对象集包装器,其中将检查AC。

主要目标是进行此更改,保留现有的数据库访问代码,使用linq实现所有类的实体(数据库没有集中层)。

创建的ObjectSetWrapper是这样的:

public class ObjectSetWrapper<TEntity> : IQueryable<TEntity> where TEntity : EntityObject
{
    private IQueryable<TEntity> QueryableModel;
    private ObjectSet<TEntity> ObjectSet;

    public ObjectSetWrapper(ObjectSet<TEntity> objectSetModels)
    {
        this.QueryableModel = objectSetModels;
        this.ObjectSet = objectSetModels;
    }

    public ObjectQuery<TEntity> Include(string path)
    {
        return this.ObjectSet.Include(path);
    }

    public void DeleteObject(TEntity @object)
    {
        this.ObjectSet.DeleteObject(@object);
    }

    public void AddObject(TEntity @object)
    {
        this.ObjectSet.AddObject(@object);
    }

    public IEnumerator<TEntity> GetEnumerator()
    {
        return QueryableModel.GetEnumerator();
    }

    public Type ElementType
    {
        get { return typeof(TEntity); }
    }

    public System.Linq.Expressions.Expression Expression
    {
        get { return this.QueryableModel.Expression; }
    }

    public IQueryProvider Provider
    {
        get { return this.QueryableModel.Provider; }
    }

    public void Attach(TEntity entity)
    {
        this.ObjectSet.Attach(entity);
    }

    public void Detach(TEntity entity)
    {
        this.ObjectSet.Detach(entity);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.QueryableModel.GetEnumerator();
    }
}

它非常简单,适用于简单的查询,例如:

//db.Product is ObjectSetWrapper<Product>
var query = (from item in db.Product where item.Quantity > 0 select new { item.Id, item.Name, item.Value });
var itensList = query.Take(10).ToList();

但是当我有这样的子查询时:

//db.Product is ObjectSetWrapper<Product>
var query = (from item in db.Product
             select new
             {
                 Id = item.Id,
                 Name = item.Name,
                 SalesQuantity = (from sale in db.Sale where sale.ProductId == item.Id select sale.Id).Count()
             }).OrderByDescending(x => x.SalesQuantity);

var productsList = query.Take(10).ToList();

我得到 NotSupportedException ,说我无法创建内部查询实体类型的常量值:

  

无法创建“MyNamespace.Model.Sale”类型的常量值。   此处仅支持原始类型或枚举类型   上下文。

如何让我的查询有效?我真的不需要让我的包装器成为ObjectSet类型,我只需要在查询中使用它。


更新

我改变了我的班级签名。现在它也在实现IObjectSet&lt;&gt;,但我得到了相同的 NotSupportedException

public class ObjectSetWrapper<TEntity> : IQueryable<TEntity>, IObjectSet<TEntity> where TEntity : EntityObject

2 个答案:

答案 0 :(得分:3)

修改

问题是以下LINQ构造被转换为LINQ表达式,其中包含您的自定义类(ObjectSetWrapper)。

var query = (from item in db.Product
             select new
             {
                 Id = item.Id,
                 Name = item.Name,
                 SalesQuantity = (from sale in db.Sale where sale.ProductId == item.Id select sale.Id).Count()
             }).OrderByDescending(x => x.SalesQuantity);

LINQ to Entities尝试将此表达式转换为SQL语句,但它不知道如何处理自定义类(以及自定义方法)。

在这种情况下的解决方案是将IQueryProvider替换为自定义的,它应该拦截查询执行并将包含自定义类/方法的LINQ表达式转换为有效的LINQ to Entities表达式(它与实体和对象一起运行)集)。

表达式转换是使用从ExpressionVisitor派生的类执行的,该类执行表达式树遍历,将相关节点替换为LINQ to Entities可接受的节点

第1部分 - IQueryWrapper

// Query wrapper interface - holds and underlying query
interface IQueryWrapper
{
    IQueryable UnderlyingQueryable { get; }
}

第2部分 - 摘要QueryWrapperBase(非通用)

abstract class QueryWrapperBase : IQueryProvider, IQueryWrapper
{
    public IQueryable UnderlyingQueryable { get; private set; }

    class ObjectWrapperReplacer : ExpressionVisitor
    {
        public override Expression Visit(Expression node)
        {
            if (node == null || !typeof(IQueryWrapper).IsAssignableFrom(node.Type)) return base.Visit(node);
            var wrapper = EvaluateExpression<IQueryWrapper>(node);
            return Expression.Constant(wrapper.UnderlyingQueryable);
        }

        public static Expression FixExpression(Expression expression)
        {
            var replacer = new ObjectWrapperReplacer();
            return replacer.Visit(expression);
        }

        private T EvaluateExpression<T>(Expression expression)
        {
            if (expression is ConstantExpression) return (T)((ConstantExpression)expression).Value;
            var lambda = Expression.Lambda(expression);
            return (T)lambda.Compile().DynamicInvoke();
        }
    }

    protected QueryWrapperBase(IQueryable underlyingQueryable)
    {
        UnderlyingQueryable = underlyingQueryable;
    }

    public abstract IQueryable<TElement> CreateQuery<TElement>(Expression expression);
    public abstract IQueryable CreateQuery(Expression expression);

    public TResult Execute<TResult>(Expression expression)
    {
        return (TResult)Execute(expression);
    }

    public object Execute(Expression expression)
    {
        expression = ObjectWrapperReplacer.FixExpression(expression);
        return typeof(IQueryable).IsAssignableFrom(expression.Type)
            ? ExecuteQueryable(expression)
            : ExecuteNonQueryable(expression);
    }

    protected object ExecuteNonQueryable(Expression expression)
    {
        return UnderlyingQueryable.Provider.Execute(expression);
    }

    protected IQueryable ExecuteQueryable(Expression expression)
    {
        return UnderlyingQueryable.Provider.CreateQuery(expression);
    }
}

第3部分 - 通用QueryWrapper<TElement>

class QueryWrapper<TElement> : QueryWrapperBase, IOrderedQueryable<TElement>
{
    private static readonly MethodInfo MethodCreateQueryDef = GetMethodDefinition(q => q.CreateQuery<object>(null));

    public QueryWrapper(IQueryable<TElement> underlyingQueryable) : this(null, underlyingQueryable)
    {
    }

    protected QueryWrapper(Expression expression, IQueryable underlyingQueryable) : base(underlyingQueryable)
    {
        Expression = expression ?? Expression.Constant(this);
    }

    public virtual IEnumerator<TElement> GetEnumerator()
    {
        return ((IEnumerable<TElement>)Execute<IEnumerable>(Expression)).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public Expression Expression { get; private set; }

    public Type ElementType
    {
        get { return typeof(TElement); }
    }

    public IQueryProvider Provider
    {
        get { return this; }
    }

    public override IQueryable CreateQuery(Expression expression)
    {
        var expressionType = expression.Type;
        var elementType = expressionType
            .GetInterfaces()
            .Single(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
            .GetGenericArguments()
            .Single();

        var createQueryMethod = MethodCreateQueryDef.MakeGenericMethod(elementType);

        return (IQueryable)createQueryMethod.Invoke(this, new object[] { expression });
    }

    public override IQueryable<TNewElement> CreateQuery<TNewElement>(Expression expression)
    {
        return new QueryWrapper<TNewElement>(expression, UnderlyingQueryable);
    }

    private static MethodInfo GetMethodDefinition(Expression<Action<QueryWrapper<TElement>>> methodSelector)
    {
        var methodCallExpression = (MethodCallExpression)methodSelector.Body;
        return methodCallExpression.Method.GetGenericMethodDefinition();
    }
}

第4部分 - 最后是您的ObjectSetWrapper

public class ObjectSetWrapper<TEntity> : IQueryable<TEntity>, IQueryWrapper where TEntity : class
{
    private IQueryable<TEntity> QueryableModel;
    private ObjectSet<TEntity> ObjectSet;

    public ObjectSetWrapper(ObjectSet<TEntity> objectSetModels)
    {
        this.QueryableModel = new QueryWrapper<TEntity>(objectSetModels);
        this.ObjectSet = objectSetModels;
    }

    public ObjectQuery<TEntity> Include(string path)
    {
        return this.ObjectSet.Include(path);
    }

    public void DeleteObject(TEntity @object)
    {
        this.ObjectSet.DeleteObject(@object);
    }

    public void AddObject(TEntity @object)
    {
        this.ObjectSet.AddObject(@object);
    }

    public IEnumerator<TEntity> GetEnumerator()
    {
        return QueryableModel.GetEnumerator();
    }

    public Type ElementType
    {
        get { return typeof(TEntity); }
    }

    public System.Linq.Expressions.Expression Expression
    {
        get { return this.QueryableModel.Expression; }
    }

    public IQueryProvider Provider
    {
        get { return this.QueryableModel.Provider; }
    }

    public void Attach(TEntity entity)
    {
        this.ObjectSet.Attach(entity);
    }

    public void Detach(TEntity entity)
    {
        this.ObjectSet.Detach(entity);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.QueryableModel.GetEnumerator();
    }

    IQueryable IQueryWrapper.UnderlyingQueryable
    {
        get { return this.ObjectSet; }
    }
}

答案 1 :(得分:0)

您的内部查询失败,因为您应该在遍历外键时引用另一个数据集:

SalesQuantity = item.Sales.Count()