我怎样才能从
Expression<Func<T, bool>> predicate
到
Expression<Func<SomeType, bool>> predicate
?
到现在也没找到方法。或者至少Expression<Func<SomeType, bool>>
通过使用谓词的第一个字符串表示来创建一个新的。
如果它有帮助,T
则仅限于实现的类型ISomeInterface
,并SomeType
实现它。
LE:进一步澄清
界面类似于:
public interface ICacheable
{
List<T> GetObjects<T>(Expression<Func<T, bool>> predicate) where T : ICacheable;
}
那么你有
public partial class Video : ICacheable
{
public List<T> GetObjects<T>(Expression<Func<T, bool>> predicate) where T : ICacheable
{
// implementation here that returns the actual List<Video>
// but when I try to query the dbcontext I can't pass a predicate with type T, I have to cast it somehow
List<Video> videos = db.Videos.Where(predicate).ToList(); // not working
}
}
那么你有:
public class RedisCache
{
public List<T> GetList<T>(Expression<Func<T, bool>> predicate) where T : ICacheable
{
List<T> objList = // get objects from cache store here
if(objList == null)
{
List<T> objList = GetObjects<T>(predicate);
// cache the result next
}
return objList;
}
}
我从任何类中使用上述内容,如下所示:
// If the list is not found, the cache store automatically retrieves
// and caches the data based on the methods enforced by the interface
// The overall structure and logic has more to it.
List<Video> videos = redisCache.GetList<Video>(v => v.Title.Contains("some text"));
List<Image> images = redisCache.GetList<Image>(v => v.Title.Contains("another text"));
我会将其扩展到我需要可缓存的任何类型的对象,如果在缓存中找不到实体或实体列表,则允许缓存存储自动检索实体或实体列表的方法。不过,我可能做错了。