存储库模式中的分页、排序等在哪里?
在 ASP.NET 存储库模式项目中,我们将数据分页和排序的逻辑放在哪里?
应该放在服务层还是放在控制器中,让控制器直接调用存储库?控制器-> 此处显示了 jquery 网格的存储库。
但与那篇文章不同的是,我的存储库返回 IQueryable
Where do we put the logic for paging and sorting data in an asp.net repository pattern project?
Should it go in the service layer or put it in the controller and have the controller directly call the repository? Controller -> Repository is shown here for a jquery grid.
But unlike that article, my repository returns IQueryable<datatype>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的存储库返回物化序列(
ICollection
、List
)等,则它应该进入存储库。但是如果您返回
IQueryable;
(就像我一样),我希望您在控制器和存储库之间有一个服务层,它在 IQueryable 上执行查询并将它们具体化为具体的集合。所以,我会把分页放在服务层。
像这样的东西:
.ToPagedList
可以是一种扩展方法,它应用您需要的分页并将项目投影到PagedList
。有许多
PagedList
实现。我喜欢Rob Conery 的。具有您的视图所需的属性,因此您可以绑定到PagedList
并创建一个 HTML Helper 来非常轻松地呈现页码。任何分页列表 LINQ 实现的唯一问题是Count()
操作(记录数)需要在服务器上完成,因此会导致 2 次往返。您不希望将非具体化查询返回到您的视图,因为 IMO 这打破了 MVC 模式。
每次转到新页面时,调用您的服务来检索所需的结果。
It should go in the Repository if your Repository returns materialized sequences (
ICollection<T>
,List<T>
), etc.But if your returning
IQueryable<T>
(like i am), i hope you have a service layer mediating between your Controllers and Repository, which executes queries on the IQueryable and materialises them into concrete collections.So, i would put the paging in the service layer.
Something like this:
.ToPagedList
can be an extension method that applies the paging you require and projects to aPagedList<T>
.There are many
PagedList<T>
implementations out there. I like Rob Conery's one. Has properties that your View requires, so you can bind to aPagedList<T>
and create a HTML Helper to render out the page numbers very easily. The only problem with any paged list LINQ implementation is theCount()
operation (for the number of records) needs to be done on the server and hence results in 2 round trips.You don't want to be returning non-materialized queries to your View's, as IMO this breaks the MVC pattern.
Each time you go to a new page, call your service to retrieve the required result.
排序和分页是数据功能。它应该进入存储库。
Sorting and paging are data functions. It should go in the repository.
存储库应该返回一个“PageableResultSet”或类似的东西,它负责分页。
The repository should return a 'PageableResultSet', or something like that, which is responsible for the paging.
我认为分页应该放在存储库中。请参阅我在另一个问题上发布的答案。
存储库和持久性再次无知
I believe paging should go in the repository. See this answer I posted on another question.
Repositories and persistence ignorance again