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

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

问题描述

我首先使用EF4代码,并有持久化的存储库和与之交互的服务层。我有一个服务层方法,在我的存储库中调用一个IQueryable方法,并返回一个包含实体的IEnumerable。我还需要返回总记录数,以便我可以计算分页链接。



如何从我的服务方法返回int和IEnumerable?




  • 在总计数的方法上使用out参数

  • 创建一个单独的类,其中包括总行作为属性计数$ / b $ b
  • 将分页LINQ查询从服务层移出(从服务层上的repo公开IQueryable)

  • 创建一个完全单独的方法在服务层上执行一个新的查询只是为了计数。



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



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



Repo图层: / strong>



返回DbSet的IQueryable,从表示层抽取数据库访问



服务层:



在IQueryable上执行LINQ查询以过滤,只需使用skip即可获取页面项,并返回一个IEnumerable(也可以设置为List,以避免任何DbContext终身问题)



演示层



调用服务层上的方法(getPagedResults(filters,pageNumber,pageSize))



从外观上我还需要添加一个单独的方法来获取总结果。希望能在一个电话中做到这一点。



我不希望将所有记录带回到演示文稿,然后页面似乎效率不高。

解决方案

你可以这样做

  public class Repository< TEntity> 
{
public IEnumerable< TEntity> GetCollection(Expression< Func< TEntity,bool>>> filter,
int pageSize,int pageIndex)
{
返回YourDbSet.Where(filter).OrderBy(sortExpression).Skip(pageSize *的PageIndex)。取(pageSize的);
}

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

然后你可以编写一个扩展方法,这些方法

  public static Pagination< TEntity> GetPagination< TEntity>(此Repository< TEntity>存储库,
表达式&FunC< TEntity,bool>>> filter,int pageSize,int pageIndex)
{
var entities = repository.GetCollection过滤器,pageSize,pageIndex);
var count = repository.Count(filter);

返回新的分页(实体,pageSize,pageIndex + 1,count);
}

这样可以重复使用 GetCollection 计数方法。
您可以动态构建where条件。看看我的答案


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.

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

  • Use a out parameter on the method for the total row count
  • Create a separate class that includes the total row count as a property
  • Move the paging LINQ query out of the service layer (expose the IQueryable from the repo on the service layer)
  • Create a full separate method on the service layer that does a new query just for count.

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

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)

Repo layer:

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

Service layer:

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 layer:

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

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.

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

解决方案

You can do something like this

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);
}

This way you can reuse GetCollection and Count methods independently. You can build the where condition dynamically. Take a look at my answer

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

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