我应该如何从暴露我的服务层的方法的记录总数和分页记录IEnumable收藏? [英] How should I expose the total record count and IEnumable collection of paged records from my service layer method?

查看:317
本文介绍了我应该如何从暴露我的服务层的方法的记录总数和分页记录IEnumable收藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用带有code EF4和具有持久性和与之交互的服务层的存储库。我有要求我的资源库中的IQueryable方法,并返回包含实体的IEnumerable服务层的方法。我还需要返回的记录总数,所以我可以计算出分页链接。

I am using EF4 with code first and have a repository for persistence and a service layer that interacts with it. I have a service layer method that calls a IQueryable method on my repository and returns a IEnumerable containing the entities. I also need to return the total record count so I can calculate the paging links.

应该如何从我的服务方法返回的INT和IEnumerable两者兼而有之?

How should I return both the int and IEnumerable from my service method?


  • 使用一个out参数的方法总行数

  • 创建一个单独的类,其中包括总行数作为一个属性

  • 将分页LINQ查询了服务层(从服务层上回购揭露IQueryable的)的

  • 创建,做一个新的查询只是计数的服务层上一个完整的独立的方法。

所有这些应该工作,但哪一个是干净的?

All of these should work, but which one is the cleanest?

更新:下面是一些澄清架构。如果这是错误的,那么请告诉我更好的方法(例如 - 做分页在presentation层,而不是服务层等)

UPDATE: Here is some clarification of the architecture. If this is wrong, then please tell me better way (eg - do the paging in the presentation layer instead of service layer,etc)

回购层:

返回DbSet的IQueryable的,抽象从presentation层DB访问

returns IQueryable of DbSet, abstracts the db access from the presentation layer

服务层:

确实对IQueryable的LINQ查询来筛选和刚刚获得页面的项目,如使用跳跃,并采取必要和返回的IEnumerable(打算也设定为名单上的回报,以避免任何的DbContext寿命问题)

does a LINQ query on the IQueryable to filter and just get the page items as needed using skip and take and returns a IEnumerable (going to also set to List on return to avoid any DbContext lifetime issues)

presentation层:

调用方法在业务层(getPagedResults(过滤器,PAGENUMBER,pageSize的))

Call the method on the Service layer (getPagedResults(filters, pageNumber, pageSize))

从外观上来看我还需要添加一个单独的方法来获得总成绩。被hopeing做到这一切在一个电话。

From the looks of it I will also need to add a separate method to get the total results. Was hopeing to do this all in one call.

我想preFER不带回所有的记录,以presentation然后网页...似乎效率不高。

I would prefer not to bring back all the records to presentation and then page... seems inefficient.

推荐答案

您可以做这样的事情。

public class Repository<TEntity>
{
   public IEnumerable<TEntity> GetCollection(Expression<Func<TEntity, bool>> filter, 
      int pageSize, int pageIndex)
   {
      return YourDbSet.Where(filter).OrderBy(sortExpression).Skip(pageSize * pageIndex).Take(pageSize);
   }

   public int Count(Expression<Func<TEntity, bool>> filter)
   {
      return YourDbSet.Where(filter).Count();
   }
}

然后你可以写一个扩展方法使用这两种方法

Then You can write an extension method to use both of these methods

public static Pagination<TEntity> GetPagination<TEntity>(this Repository<TEntity> repository, 
   Expression<Func<TEntity, bool>> filter, int pageSize, int pageIndex)
{
   var entities = repository.GetCollection(filter, pageSize, pageIndex);
   var count = repository.Count(filter);

   return new Pagination(entities, pageSize, pageIndex + 1, count);
}

这样,您就可以重复使用 GetCollection 计数方法独立完成。
您可以动态地建立WHERE条件。看看我的<一个href=\"http://stackoverflow.com/questions/6401712/how-to-make-a-compound-or-clause-in-linq/6401902#6401902\">answer

这篇关于我应该如何从暴露我的服务层的方法的记录总数和分页记录IEnumable收藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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