views:

85

answers:

5

I am creating a search results page in C# in an ASP.NET 1.1 page. In my data layer I have a DataSet that stores the result of a plain old ADO.NET stored procedure call. The DataSet has two DataTables and I'm using a DataVIew to filter and sort the columns. I only want to fill the dataset once, and then work on the DataTables and derived DataView until the page is unloaded. How best should I cache the DataSet in my DAL so that it is filled only PageLoad? Do i put it in a Cache object, a static member variable, a property...I don't have any fancy entity models or ORM, this is .NET 1.1. Thanks in advance.

A: 

A static variable in a class should work just fine. (Set it in the page load.)

Hogan
I've tried this and it works fine for getting the app running. Does keeping a reference to the dataset in a static variable mean the dataset is cached in memory for all users or just the current user?
Zener Diode
Zener: Just for the page life-cycle as you say in your question. All these other answers are keeping the page around for longer than the page.
Hogan
+1  A: 

there are ORMs that work with .net 1.1, but if you don't want to use them, I would recommend either populating something in the Cache so that you can expire it if needed and re-get the data. You could also put it into the session (if the data is user specific) or application (if it is not) objects.

David
Out of interest David what ORM would you recomment?
Zener Diode
NHibernate 1.0 was written to .net 1.1
David
+1  A: 

Will your DAL be used by any other applications? If not then you can imbed the caching of the DataSet in the Cache or Session depending on what features are need of the storage.

Session will be specific to the user and get cleaned up when the user's session expires which means the data might be around a lot longer than required. More info on Session

Cache is nice because it will auto expire (use sliding expiry if the search data will not change often) and you can store the search criteria with it so that other users can leverage the search as well which will save you calls to the DB by multiple users possibly. Multiple user's being able to access this data is a big advantage over using Session. More info on Cache

If you plan to use your DAL in other apap, you might want the application to do the caching itself.

You just need a wrapper like:

// Consider this psuedo code for using Cache
public DataSet GetMySearchData(string search)
{
    // if it is in my cache already (notice search criteria is the cache key)
    string cacheKey = "Search " + search;
    if (Cache[cacheKey] != null)
    {
        return (DataSet)(Cache[cacheKey]);
    }
    else
    {
        DataSet result = yourDAL.DoSearch(search);
        Cache[cacheKey].Insert(result);  // There are more params needed here...
        return result;
    }
}
Kelsey
I dont think I'll be using this DAL again but I want to minimize the round-trips to the db and make the search results avaiable to multiple users. The original dataset is common to all users though they will be able to filter it via a DataView. Thanks for the help.
Zener Diode
A: 

My thoughts

As you are populating 2 datatables once , If the data that you are populating is not changing and same for all users of the application and if main functionality provided by these tables are look ups.

i would say that , you can create a XML data files for these tables and populate them once your application starts and store them in application objects.

sothat it will be there for life of the application.

if u need user level static data then use session variables and store the tables n session variables.

saurabh
Though the query is readonly the data will be changing quite often and could be the same for all users. I dont see the benefit in this case of serializing to XML.
Zener Diode
A: 

If this is per user data don't cache a large amount of data putting it into session or similar, you are seriously hindering the ability of your site to scale. Consider how the amount of info in memory grows as you add more users, and then what happens when you need to use more than a single site server.

Instead modify the procedure so you can only retrieve the page of data that you need to show.

If this is data that is used for all users, definitely cache it. You can use the asp.net Cache for that.

eglasius