This is a migrated thread and some comments may be shown as answers.

Web API data paging

4 Answers 158 Views
Web Services
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brian
Top achievements
Rank 2
Brian asked on 02 Jan 2013, 09:15 PM
HI,

Is it possible to retrieve paged data from the Web API generated by OpenAccess ORM?

4 Answers, 1 is accepted

Sort by
0
Dimitar Tachev
Telerik team
answered on 07 Jan 2013, 05:54 PM
Hi Brian,

Retrieving paged data from a Web API service generated by OpenAccess ORM is not currently supported out of the box but we are planning to implement it in some of our future versions.

For your convenience I prepared a bare bone project demonstrating a workaround that is using methods instead of the canonical OData queries.

The sample is using the Northwind database and the Dynamic LINQ library. There are two manually added methods in the OpenAccessBaseRepository partial class - one to get the records without detaching them and one to detach records. Also there are two added overloads of the GetAll method in the OpenAccessBaseApiController partial class:

  • Get paged data without sorting:
GetAll(int skip, int take)
You could call that method like: http://host:port/api/controller?skip=value&take=value

  • Get paged data sorted by custom properties:
GetAll (int skip, int take, string orderBy)
You could call that method like: http://host:port/api/controller?skip=value&take=value&orderBy=clause

The methods are returning a new custom object which is containing a result set of records and their total number in the database (on which you could base the paging).

At the attached images you could find the new classes and examples for using the above-mentioned methods.

Please bear in mind that some of the browsers cannot serialize the result JSON, so you could use the fiddler tool to debug the Web API services.

I hope this is applicable for you.  

Regards,
Dimitar Tachev
the Telerik team
Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
0
Brian
Top achievements
Rank 2
answered on 08 Jan 2013, 03:37 PM
Appreciate the response. Will give it a try.

Brian
0
Dat
Top achievements
Rank 1
answered on 10 Jul 2013, 06:38 AM
Dear Dimitar Tachev,

I've stumble into your post while I am searching for OpenAccess - Web Api Paging.

Question:
1. I am using Telerik.OpenAccess (Version 2013.2.611.1) has this paging been fix?, I was trying to take the sample code and paste it inside my project to implement skip/take but there are a lot of changes.

2. I am getting Out of Memory in the Following Code in OpenAccessBaseRepository.cs
I know that it is try to return all records (186,137)
How can stop the user from requesting GetAll , if it is too many record ?

public virtual IQueryable<TEntity> GetAll()

{

List<TEntity> allEntities = dataContext.GetAll<TEntity>().ToList();


I am using C# Client to Consume the Web API from the following Sample.
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

3.

HttpClient client = new HttpClient();

client.BaseAddress = new Uri("http://localhost:12345/");

client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response = client.GetAsync(string.Format("api/DocList/{0}", this.textBoxId.Text)).Result;


Result Return 1 Row the Following is Working:
var p = response.Content.ReadAsAsync<DocList>().Result;

Result Return 1 Row the Following is NOT WORKING (Following the Sample in www.asp.net .. above) :
var docs = response.Content.ReadAsAsync<IEnumerable<DocList>>().Result;

// Parse the response body. Blocking!

foreach (var p in docs)

{

   this.label1.Text = string.Format("{0}\t{1};\t{2}", p.Id, p.description, p.createdate);

}


Could you please point me to a sample of ASP.NET MVC C# Grid Consuming Web API.

Thank you

Regards Dat.

0
Dimitar Tachev
Telerik team
answered on 11 Jul 2013, 02:55 PM
Hi Dat,

 
Please find the answers to your questions below:

1) The ASP.NET Web API paging is still not implemented out of the box but the sample application should be still applicable for this scenario because there are not any breaking changes in our Web API service generation.

For you convenience I prepared another example demonstrating this approach using our latest release - the Q2 2013 SP1 - please find it attached.

2) The out of memory exception is caused by the materialization of your query containing too many records. 

In order to avoid this exception I suggest you add a limitation of the records returned as the one below:

public virtual IQueryable<TEntity> GetAll()
{
    List<TEntity> top100Entities =
dataContext.GetAll<TEntity>().Take(100).ToList();
 
    List<TEntity> detachedEntities = dataContext.CreateDetachedCopy<List<TEntity>>(top100Entities, fetchStrategy);
 
    return detachedEntities.AsQueryable();
}
Choosing this approach only the top 100 or the number that you want will be materialized from the query to the memory - you could find this method updated in the attached sample application.

3) Reviewing the code that you provided us I suppose that this issue is caused by the response object initialization and usage. It is initialized as:
HttpResponseMessage response = client.GetAsync(string.Format("api/DocList/{0}", this.textBoxId.Text)).Result;
where the service call is returning only the DocList entity having the Id provided by the textBoxId text. In this way the proper way for getting the result of the response object is using
.ReadAsAsync<DocList>()
method because the service is returning only one DocList object.

If you need to use the
.ReadAsAsync<IEnumerable<DocList>>()
method I suggest you use the
api/DocList/
URL in order to call the GetAll method (or one of its new overloads in order to get a paged data) and get a collection of items instead of only one of them.
 
Regarding a sample application demonstrating the Telerik OpenAccess ORM integration with the ASP.NET Web API services you could download our Samples Kit containing a lot of end-to-end sample applications and take a look at its ASP.NET Web API and ASP.NET MVC categories.

I suggest you take more attention at the ASP.NET Web API with WPF MVVM example which is using a very similar approach and client for consuming the Web API service.

I hope this helps. Do not hesitate to contact us back if you need any further assistance.

Regards,
Dimitar Tachev
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Tags
Web Services
Asked by
Brian
Top achievements
Rank 2
Answers by
Dimitar Tachev
Telerik team
Brian
Top achievements
Rank 2
Dat
Top achievements
Rank 1
Share this question
or