我知道这可能是个看法问题,但我正在寻找最佳实践。
据我了解,IQueryable<T>
实现了 IEnumerable<T>
,因此在我的数据访问层(DAL)中,我当前有以下方法签名:
IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProductsByCategory(int cateogoryId);
Product GetProduct(int productId);
在这里,我应该使用 IQueryable<T>
吗?
两种方法的利弊是什么?
请注意,我打算使用Repository模式,所以我会有一个类似于以下的类:
public class ProductRepository {
DBDataContext db = new DBDataContext(<!-- connection string -->);
public IEnumerable<Product> GetProductsNew(int daysOld) {
return db.GetProducts()
.Where(p => p.AddedDateTime > DateTime.Now.AddDays(-daysOld ));
}
}
我应该将我的IEnumerable<T>
更改为IQueryable<T>
吗?这两者之间有什么优缺点?