views:

73

answers:

1

I have been pondering for a while how to best cache IQueryables. I am using a repository pattern to fetch data into a class such as this one:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IQueryable<Category> { get; set; } // For simplicity. It is actually a LazyList<Category> containing an IQueryable
}

Now how would I best go about to cache this object? I would like to keep the deferred execution on the queryable as in reality there are many more things besides Category that can be linked to the item, and I would preferably not fetch them to store on the object as the objects can get too big, or they could need to be cached separately.

I am thinking a Compiled Query could make this possible somehow, I'm just not quite seeing how. Has anyone found any good solution to this? It seems to be a pretty common pattern to use.

+1  A: 

Using a compiled query, you would cache the resulting delegate, of type Func<MyDataContext, IQueryable<Category>>. Whenever you want a new queryable, you just create a new data context to pass into the delegate and query against the resulting IQueryable<Category> like normal.

dahlbyk