Monday, March 26, 2012

Preloading Data With AJAX

i have seen what ajax can do and im impressed. I do have one question.

is it possible to "preload" some data into a dataset or local objects (ie classes or collections) "behind the scenes"

For example. I have a home page and a products page. Generally you go to the products page, it checks to see if theres a Collection in memory, if not it goes to the SQL Database to get the products. This usually happens on Page Load event.

Would it be possible to make a call to the Sql DataBase from the Main page (or any page for that matter) behind the scenes so i can load the products recordsets into memory while the user is visiting the first page (or when the app starts)

Is this something ajax can do?

TIA

mcm

You could use an ajax webrequest to a webservice to on the server from your home page. The webservice would load your data and put it somewhere like session, so that it is easily retrieved again. If the data isn't specific to the user you should use Cache.

That's just one approach -- if the data is global to all users a more traditional caching mechanism would be simplier and sufficient. Check cache.. if its not there retrieve it and stuff in into cache.. otherwise get it from cache. You can even invalidate cache data with a Sql dependency. I don't have any links to give at the moment, but a few searches should turn up some good examples.


thanks for the response.

The data in question is probably better managed by caching. Its the product listings for the online catalogue. So it wont change for the session specifically.
i DO have a request bin thats handled in the Session (via facade and a SessionManager class and various other classes pertaining to a shopping cart.)

This is my first AJAX enabled website and i have to page a datalist. Im having trouble really deciding

a) where to store all the product data? and
b) how to page the dataList properly.

All the tutorials i have seen rely heavily on repeated calls to the sql server using new features of SQL Server 2005. Im using 2000 so repeated calls are
not the idea situation. I was really hoping to just make a call to get all the products when a user initiates a new visit. Then rely on a DataSet (or some equilavent)
to display whatever records need to be shown in my custom DataList which is the bulk of all the product interaction.

By now im sure you can tell im facing some issues with separating a data layer for this site. Im working my way through it however, and im sure it will all work out
shortly, but any other ideas you may have are greatly appreciated.

thanks,

mcm


It sounds like you just need to put the data into the ASP.NET Cache. Then you can use the entire set of data to bind to the DataList, and with paging enabled it will know how to select the right range of records. If you put the DataSet itself in Cache, just keep in mind that multiple users may be accessing it at the same time, so if you are going to be doing things like sorting or filtering you should make a cloned copy of the data before doing that.

There's some built in support for scenarios like that... check out this article:

http://msdn2.microsoft.com/en-us/library/ms379559(VS.80).aspx

Specifically the section titled "Using SQL Cache Invalidation with the DataSource Control". It may not be exactly what you are looking for, but should give you an idea of where to go from here.

No comments:

Post a Comment