Understanding the implications of IQueryable<T>

来源:互联网 发布:淘宝商家店铺状态异常 编辑:程序博客网 时间:2024/05/20 19:33
 Understanding the implications of IQueryable<T>


IQueryable<T> is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries). As with all powerful features, you want to be careful with how you use it and make sure it is not abused.


It is important to recognize that returning an IQueryable<T> result from your repository enables calling code to append on chained operator methods to it, and so participate in the ultimate query execution. If you do not want to provide calling code this ability, then you should return back IList<T> or IEnumerable<T> results - which contain the results of a query that has already executed.


For pagination scenarios this would require you to push the actual data pagination logic into the repository method being called. In this scenario we might update our FindUpcomingDinners() finder method to have a signature that either returned a PaginatedList:


PaginatedList< Dinner> FindUpcomingDinners(int pageIndex, int pageSize) { }


Or return back an IList<Dinner>, and use a "totalCount" out param to return the total count of Dinners:


IList<Dinner> FindUpcomingDinners(int pageIndex, int pageSize, out int totalCount) { }
原创粉丝点击