分页,排序等在存储库模式中哪里去了? [英] Where does paging, sorting, etc go in repository pattern?

查看:57
本文介绍了分页,排序等在存储库模式中哪里去了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在哪里将用于对数据进行分页和排序的逻辑放在asp.net存储库模式项目中?

Where do we put the logic for paging and sorting data in an asp.net repository pattern project?

它应该进入服务层还是放在控制器中,然后让控制器直接调用存储库?控制器->存储库显示在此处,用于jquery网格.

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.

但与该文章不同,我的存储库返回IQueryable<datatype>

But unlike that article, my repository returns IQueryable<datatype>

推荐答案

如果您的存储库返回具体化的序列(ICollection<T>List<T>)等,则应放入存储库中.

It should go in the Repository if your Repository returns materialized sequences (ICollection<T>, List<T>), etc.

但是,如果您返回的是IQueryable<T>(就像我一样),我希望您在Controllers和Repository之间建立一个服务层,该服务层在IQueryable上执行查询并将它们具体化为具体集合.

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.

类似这样的东西:

public PagedList<T> Find(Expression<Func<T,bool>> predicate, int pageNumber, pageSize)
{
   return repository
             .Find()
             .Where(predicate)
             .ToPagedList(pageNumber, pageSize);
}

.ToPagedList可以是一种扩展方法,可将所需的分页应用到并投影到PagedList<T>.

.ToPagedList can be an extension method that applies the paging you require and projects to a PagedList<T>.

有许多PagedList<T>实现.我喜欢罗伯·科纳里(Rob Conery)的.具有View所需的属性,因此您可以绑定到PagedList<T>并创建HTML Helper以非常容易地呈现页码.任何分页列表LINQ实现的唯一问题是Count()操作(针对记录数)需要在服务器上完成,因此导致两次往返.

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 a PagedList<T> and create a HTML Helper to render out the page numbers very easily. The only problem with any paged list LINQ implementation is the Count() operation (for the number of records) needs to be done on the server and hence results in 2 round trips.

您不希望将未实现的查询返回到视图的查询,因为IMO破坏了MVC模式.

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.

这篇关于分页,排序等在存储库模式中哪里去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆