14

我有一个简单的自定义 QueryProvider,它接受一个表达式,将其转换为 SQL 并查询一个 sql 数据库。

我想在 QueryProvider 中创建一个小型缓存,用于存储经常访问的对象,这样就可以在没有数据库命中的情况下进行检索。

QueryProvider 有方法

public object Execute(System.Linq.Expressions.Expression expression)
{
    /// Builds an SQL statement from the expression, 
    /// executes it and returns matching objects
}

缓存作为这个 QueryProvider 类中的一个字段,是一个简单的通用列表。

如果我使用 List.AsQueryable 方法并将上述表达式传递给 List.AsQueryable 的 Provider 的 Execute 方法,它就不能按预期工作。看起来当一个表达式被编译时,初始的 QueryProvider 成为一个不可或缺的部分。

是否可以将表达式传递给后续的 QueryProvider 并根据需要执行表达式?

调用代码看起来模糊如下:

public class QueryProvider<Entity>()
{
    private List<TEntity> cache = new List<Entity>();

    public object Execute(System.Linq.Expressions.Expression expression)
    {
        /// check whether expression expects single or multiple result
        bool isSingle = true;

        if (isSingle)
        {
            var result = this.cache.AsQueryable<Entity>().Provider.Execute(expression);
            if (result != null) 
                return result;
        }

        /// cache failed, hit database
        var qt = new QueryTranslator();
        string sql = qt.Translate(expression);
        /// .... hit database
    }
} 

它不会返回错误,而是卡在循环中,一遍又一遍地调用同一个提供程序。

这是更多代码,显示了我正在尝试做的事情:

收藏:

class Collection<Entity>
{

    internal List<Entity> cacheOne { get; private set; }
    internal Dictionary<Guid, Entity> cacheTwo { get; private set; }

    internal Collection()
    {
        this.cacheOne = new List<Entity>();
        this.cacheTwo = new Dictionary<Guid, Entity>();
    }

    public IQueryable<Entity> Query()
    {
        return new Query<Entity>(this.cacheOne, this.cacheTwo);
    }

}

询问:

class Query<Entity> : IQueryable<Entity>
{
    internal Query(List<Entity> cacheOne, Dictionary<Guid, Entity> cacheTwo)
    {
        this.Provider = new QueryProvider<Entity>(cacheOne, cacheTwo);
        this.Expression = Expression.Constant(this);
    }

    internal Query(IQueryProvider provider, Expression expression)
    {
        this.Provider = provider;
        if (expression != null)
            this.Expression = expression;
    }

    public IEnumerator<Entity> GetEnumerator()
    {
        return this.Provider.Execute<IEnumerator<Entity>>(this.Expression);
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

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

    public System.Linq.Expressions.Expression Expression { get; private set; }

    public IQueryProvider Provider { get; private set; }
}

查询提供者:

class QueryProvider<Entity> : IQueryProvider
{

    private List<Entity> cacheOne;
    private Dictionary<Guid, Entity> cacheTwo;

    internal QueryProvider(List<Entity> cacheOne, Dictionary<Guid, Entity> cacheTwo)
    {
        this.cacheOne = cacheOne;
        this.cacheTwo = cacheTwo;   
    }

    public IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
    {
        return new Query<TElement>(this, expression);
    }

    public IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
    {
        throw new NotImplementedException();
    }

    public TResult Execute<TResult>(System.Linq.Expressions.Expression expression)
    {
        return (TResult)this.Execute(expression);
    }

    public object Execute(System.Linq.Expressions.Expression expression)
    {
        Iterator<Entity> iterator = new Iterator<Entity>(expression, cacheOne, cacheTwo);
        return (iterator as IEnumerable<Entity>).GetEnumerator();
    }
}

迭代器:

class Iterator<Entity> : IEnumerable<Entity>
{
    private Expression expression;
    private List<Entity> cacheOne;
    private Dictionary<Guid, Entity> cacheTwo;

    internal Iterator(Expression expression, List<Entity> cacheOne, Dictionary<Guid, Entity> cacheTwo)
    {
        this.expression = expression;
        this.cacheOne = cacheOne;
        this.cacheTwo = cacheTwo;
    }

    public IEnumerator<Entity> GetEnumerator()
    {
        foreach (var result in (IEnumerable<Entity>)this.cacheOne.AsQueryable<Entity>().Provider.Execute(expression))
        {
            yield return result;
        }

        foreach (var more in (IEnumerable<Entity>)this.cacheTwo.Values.AsQueryable<Entity>().Provider.Execute(expression))
        {
            yield return more;
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

程序:

class Program
{
    static void Main(string[] args)
    {
        /// Create collection + caches
        var collection = new Collection<Giraffe>();
        collection.cacheOne.AddRange(new Giraffe[] {
            new Giraffe() { Id = Guid.NewGuid(), DateOfBirth = new DateTime(2011, 03, 21), Height = 192, Name = "Percy" },
            new Giraffe() { Id = Guid.NewGuid(), DateOfBirth = new DateTime(2005, 12, 25), Height = 188, Name = "Santa" },
            new Giraffe() { Id = Guid.NewGuid(), DateOfBirth = new DateTime(1999, 04, 01), Height=144, Name="Clown" }
        });
        var cachetwo = new List<Giraffe>(new Giraffe[] {
            new Giraffe() { Id = Guid.NewGuid(), DateOfBirth = new DateTime(1980, 03,03), Height = 599, Name="Big Ears" },
            new Giraffe() { Id = Guid.NewGuid(), DateOfBirth = new DateTime(1985, 04, 02), Height= 209, Name="Pug" }
        });
        foreach (var giraffe in cachetwo)
            collection.cacheTwo.Add(giraffe.Id, giraffe);

        /// Iterate through giraffes born before a certain date
        foreach (var result in collection.Query().Where(T => T.DateOfBirth < new DateTime(2006, 01, 01)))
        {
            Console.WriteLine(result.Name);
        }

    }
}

长颈鹿:

class Giraffe
{
    public Guid Id { get; set; }
    public string Name { get; set;  }
    public long Height { get; set; }
    public DateTime DateOfBirth { get; set; }
}

特殊情况,例如 SingleAndDefault 等被忽略。我想要工作的部分发生在迭代器中,它首先在执行字典之前执行列表的查询提供程序。

两个 Queryable 对象之一可能是数据库或其他对象。

4

1 回答 1

7

不,查询不会绑定到提供程序。这就是您拥有 IQueryable 接口的原因:它同时提供了表达式和提供程序,因此 LINQ 可以调用提供程序来执行表达式。

您的实现中的问题在Query<Entity>于其自身的表现方式:您将根表达式设置为,查询Expression.Constant(this)在哪里(而不是集合)。this

因此,当您使用 LINQ-to-Objects 执行查询时,它会调用GetEnumeratoron Query<>,然后调用 LINQ-to-Objects 来执行Expression,它有一个根表达式Expression.Constant(this)(类型为Query<>),然后 LINQ-to-Objects 迭代这个根表达式通过调用GetEnumeratorthisQuery<>

问题在于

(IEnumerable<Entity>)this.cacheOne.AsQueryable<Entity>().Provider.Execute(expression)

这基本上等于

new Entity[0].AsQueryable().Provider.Execute(expression)

或者

linqToObjectsProvider.Execute(expression)

查询返回的 Provider链接到源 ( this.cacheOne),因此您只是重新执行表达式,而不是查询缓存。

以下有什么问题?

class Collection<Entity>
{
    ...

    public IQueryable<Entity> Query()
    {
        return this.cacheOne.Concat(this.cacheTwo.Values).AsQueryable();
    }
}

请注意,Concat使用延迟评估,因此只有在执行查询时才将 cacheOne 和 cacheTwo 连接起来,然后使用附加的 LINQ 运算符进行操作。

(在这种情况下,我会创建Collection<Entity>一个 IQueryable withExpression equal toExpression.Constant(this.cacheOne.Concat(this.cacheTwo.Values))`。我认为您可以取消所有其他类。)


原始答案

但是,我认为这种捎带 LINQ to Objects 的方式永远无法完成您认为应该做的事情。

至少,您应该保留原始查询提供程序,以便在缓存未命中时调用该提供程序。如果你不这样做,并使用你自己的查询提供程序(你没有显示你用来进行实际调用的代码),你的查询提供程序将一次又一次地调用自己。

所以你需要创建一个 CachingQueryProvider一个 CachingQuery:

class CachingQuery<T> : IQueryable<T>
{
    private readonly CachingQueryProvider _provider;
    private readonly Expression _expression;

    public CachingQuery(CachingQueryProvider provider, Expression expression)
    {
        _provider = provider;
        _expression = expression;
    }

    // etc.
}

class CachingQueryProvider : IQueryProvider
{
    private readonly IQueryProvider _original;

    public CachingQueryProvider(IQueryProvider original)
    {
        _original = original;
    }

    // etc.
}

public static class CachedQueryable
{
    public static IQuerable<T> AsCached(this IQueryable<T> source)
    {
        return new CachingQuery<T>(
             new CachingQueryProvider(source.Provider), 
             source.Expression);
    }
}

此外,如果要缓存结果,则需要在缓存结果之前具体化结果,否则缓存查询,而不是结果。结果本身不应该再次执行,因为它已经是您应该返回的数据。

我将进入的方向如下:

class CachingQueryProvider : IQueryProvider
{
    public object Execute(Expression expression)
    {
        var key = TranslateExpressionToCacheKey(expression);

        object cachedValue;
        if (_cache.TryGetValue(key, out cachedValue))
            return cachedValue;

        object result = _originalProvider.Execute(expression);

        // Won't compile because we don't know T at compile time
        IEnumerable<T> sequence = result as IEnumerable<T>;
        if (sequence != null && !(sequence is ICollection<T>)) 
        {
            result = sequence.ToList<T>();
        }

        _cache[key] = result; 

        return result;
    }
}

对于标记为 的部分Won't compile,您将不得不做一些反射技巧。

注意:字符串实现了 IEnumerable,因此请注意不要尝试实现单个字符串结果值。

于 2012-05-25T15:03:10.363 回答