我正在使用通用存储库模式,并使用以下方法:
private ObjectQuery<T> ObjectQueryList()
{
var list = CamelTrapEntities.CreateQuery<T>(EntitySetName);
return list;
}
public IQueryable<T> List()
{
return ObjectQueryList();
}
方法List()返回IQueryable<T>,因为IQueryable<T>易于模拟。我还有一个扩展方法:
public static IQueryable<T> Include<T>(this IQueryable<T> obj, string path)
{
if (obj is ObjectQuery<T>)
(obj as ObjectQuery<T>).Include(path);
return obj;
}
这个方法是在存储库之外使用的,用于获取已经加载了导航属性的实体列表,例如:List.Include("CreatedBy")。但问题在于它不起作用。所有包含操作都会被忽略掉。当我将List()方法更改为
public ObjectQuery<T> List()
{
return ObjectQueryList();
}
一切运行正常。
我该如何实现仓储模式以执行更复杂的查询?