Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

asp.net mvc - Implementing pager through WCF service

I am developing an application which includes a WCF service and its ASP.NET MVC client. The ASP.NET MVC website must display a grid of objects - say, products. These products are stored in database which is accessible through the WCF service. So somewhere inside an MVC controller I call WCF service's method that returns me an array of products that I need to display.

So what is my question? I want to implement a pager functionality for my products grid, because it is possible that there will be a lot of products. So there are several ways to do that:

  1. My controller can get the whole list of products and just do in-memory paging
  2. WCF can select all the products and store them somewhere in its cache, then pass to the controller only part of them, according to the requested page number.
  3. WCF can select only part of the products from the database, according to the requested page number.
  4. WCF can return IQueryable to the controller, and then the controller will select whatever he wants whenever he wants.

As far as I understand (and correct me if it is not true), the first option is useless, so I must choose between the others.

The second option wastes my server's memory.

The third option is OK, but it seems a little bit ugly to implement paging on the WCF side.

And the fourth option sounds confusing. I actually pass some kind of query to the client, and then he queries my database by himself, through the WCF service. I can't figure out how to implement this correctly.

So can you please help me to choose the correct way to implement this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What is your back-end database layer look like? If you're using LINQ (-to-SQL or -to-Entities), you could implement paging through WCF by specifying the page size and the page number you want, and then use LINQ's "Skip" and "Take" operators to fetch the page requested - something roughly like:

[ServiceContract]
public interface IFetchData
{
  [OperationContract]
  public List<Data> GetData(int pageSize, int pageNumber)
}

and then implement it something like this (simplified):

public class FetchDataService : IFetchData
{
  public List<Data> GetData(int pageSize, int pageNumber)
  {
      var query = yourContext.DataTable
                    .Skip((pageNumber - 1) * pageSize)
                    .Take(pageSize);

      return query.ToList();
  }
}

Would that be helpful for you??

Marc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...