Extract information from a Func<bool, T> or alike lambda
I''m trying to build a generic cache layer. ICacheRepository
Say I have the following:
public class Person
{
public int PersonId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime Added { get; set; }
}
And I have something like this:
list.Where(x => x.Firstname == "Syska");
Here I want to extract the above information, to see if the query supplied the "PersonId" which it did not, so I dont want to cache it.
But lets say I run a query like this:
list.Where(x => x.PersonId == 10);
Since PersonId is my key ... I want to cache it. with the key like开发者_StackOverflow社区 "Person_10" and I later can fetch it from the cache.
I know its possible to extract the information with Expression<Func<>>
but there seems to be a big overhead of doing this (when running compile and extract the Constant values etc. and a bunch of cache to be sure to parse right)
Are there a framework for this? Or some smart/golden way of doing this ?
Updated information:
I'm using EF Code First CTP5.
When a entity is accessed through my Repository and queried only on some specific properties on my data class, I want to save it to the cache.
So when doing:
repository.SingleOrDefault(x => x.Email == "some@legel.mail");
And Email is my cache key for the "User" entity, I want my repository to cache it, because its being queried by "Email".
Expression<Func<User, bool>> b;
b.Body // Here I can extract most information without compiling it, to fetch what its being quried for.
Its not that I can't create a Expression parser, but as you says, there are many edge cases, where the existing SQL providers are way better. But thats not what I'm trying to do here.
I simple want to extract the right side of the BinaryExpression .... the best and fastest way. Since compiling every query is slow.
Even better I just want a cache layer for EF Code First.
In order to do this, you actually want to process Expression<Func<bool, T>>
. This contains the necessary metadata (in the form of expression objects) to allow you to inspect what was actually specified in the query. This is how ORMs like LINQ-to-SQL and the Entity Framework perform the translation from your strongly-typed query expressions into their respective query layers (LINQ-to-SQL straight to SQL, Entity Framework to EntitySQL, etc.).
There are two ways for you to get at this information.
- Implement
IQueryable<T>
. This is the standard querying interface that all LINQ providers (such as those listed above) implement. - Declare a function called
Where
on your type that takesExpression<Func<bool, T>>
as its argument.
Either way, you'll get the expression. You can then do whatever you like to it, including compiling it and executing it as normal code.
If you want to get started, head over to MSDN and take a peek. There's a LOT of information, but it should be able to answer any questions you have. If not, you already know how to ask one here!
It might be preferable to define your own extension method Person GetById(this List<Person> list, int personId)
. The cache will only be effective as long as the programmer remembers to call the special method, but it will perform much better.
精彩评论