综合排序/分页/过滤服务层接口方法
我正在尝试编写一个服务层方法,该方法将采用所有必要的参数,使我能够以 Webforms 网格的工作方式从存储库获取数据。
存储库返回 IQueryable
。
我想出了一个这样的模型:
public class PagedModel<T>
{
public GridSortOptions GridSortOptions { get; set; } //Enum for ASC and DESC
public IList<T> Items { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
}
方法:
PagedModel<User> GetUsers(Expression<Func<T,bool>> predicate, int page, int pageSize, GridSortOptions sortOption);
问题:
- 这个方法中我缺少哪些参数?
- PagedModel 中缺少哪些属性?
- 我该如何实施该方法?
我想要的是:
- 分页
- 排序
- 过滤
I'm trying to write a service layer method that will take all the necessary parameters that will allow me to get data from the repository in the way how a webforms grid would work.
The repository returns IQueryable<T>
.
I came up with a model like this:
public class PagedModel<T>
{
public GridSortOptions GridSortOptions { get; set; } //Enum for ASC and DESC
public IList<T> Items { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
}
The method:
PagedModel<User> GetUsers(Expression<Func<T,bool>> predicate, int page, int pageSize, GridSortOptions sortOption);
Questions:
- What parameters am I missing from this method?
- What properties am I missing from the PagedModel?
- How do I implement the method?
What I want:
- Paging
- Sorting
- Filtering
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现有时创建一个“Criteria”类很好,其中包含与您正在搜索的项目列表分开的所有属性。这样,您可以将其用作模型属性,该属性将自动绑定到您的搜索操作,将其传递给服务和存储库方法(而不是一堆单独的参数),如果需要,将其保留在会话中,具有强类型过滤器属性特定于您正在搜索的当前类型(即用户),有一个用于保存通用分页或排序的超类型。
像这样的东西
I find it nice sometimes to create a "Criteria" class that contains all the properties separate from the list of items you are searching for. This way you can use it as a model property that will automatically be bound on your search actions, pass it to service and repository methods (instead of a bunch of separate params), persist it in session if need be, have strongly typed filter properties specific to the current type you are searching for (ie User), have a supertype for holding generic paging or sorting.
Something like
我刚刚使用了 Telerik MVC 组件,它们是开源且免费的。我刚刚发现的最好的事情是,如果您使用
IQueryable
作为数据源,它会自动为您提供...分页、排序、过滤和分组(当然有一些位我已遗漏)直接开箱即用!也许值得一看。
I've just been using the Telerik MVC components, they're open source and free. The nicest thing I've just found is that if you use
IQueryable<T>
as your datasource, it will automagically provide you with... paging, sorting, filtering and grouping (sure there's some bits I've left out) straight out of the box!It might be worth looking at them.