简体   繁体   中英

How to preserve the data in auto property in service for every request send to Web API for particular user? (not in session)

There is more than 60,000 records in the table. I am using Microsoft SQL Server 2016 (RTM-GDR) .

I have planned to store the data into the DataTable property and fetch only the top 100 out of the data available in the Datatable property at a time. And then delete this top 100 records so that processing would be better for each time.

Service Code

public DataTable Records { get; set; }

In Service Method

if(this.Records == null || this.Records.Count() == 0) 
{
     //For the first time add records to the `Records` data table.
}
else {
  //The Records already there.
 }

Web API Code

[HttpGet]
public HttpResponseMessage GetReports()
{
  var tempReports = this.mService.GetReports();
  if (tempReports == null)
  {
    return ErrorResult(HttpStatusCode.NotFound);
  }

  return OK(tempReports );
}

Problem

This this.Records.Count() is 0 always whenever I send new request to fetch the data.

The data is getting successfully added to the Records data table but it's not being preserved .

Is there something I have to do explicitly from the Web API to preserve the records for the particular user ?

First of all, Web Api is stateless and each request is supposed to be completely independent from the previous request and if you have chosen a stateless technology you'd better be loyal to its specifications.
Regarding your concern of performance, from your code I guess that you want to do something like pagination. Let me provide you with a sample code by which pagination is normally performed using entity framework:

var pagedList = yourDbContext.youEntityDbSet.OrderBy(x => x.Id)
                                .Skip(numberOfRecordToSkip)
                                .Take(numberOfRecordToTake)
                                .ToList();

numberOfRecordToSkip and numberOfRecordToTake need to be added to the request from client-side and don't worry about the performance, since one database connection on each request is not a big deal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM