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"));
我將擴大這對任何類型的對象我需要超高速緩存,與方法,將允許緩存存儲自動檢索一個實體或列表如果它們沒有在緩存中找到,則爲實體。但我可能會完全錯誤。
那麼你會想到,如果它獲得通過ISomeInterface'的'實現這*不*'SomeType'發生? –
關於第二個想法,我不確定這是否是一個騙局,但是無論如何,你可能想要閱讀這個問題。如果你需要做更多的事情而不是愚蠢的類型替代,我的哀悼。 – Jon
@Jon它確實是一個確切的重複。 – usr