Caching objects

J

Joel Barsotti

My website has different products and I've developed a nice little object
oreinted class library for them. I then use the objects for products and
such on many different pages. Especially with some products inheriting from
other objects and many of them needing to access the database to initialize
correctly I've created quite the load on my sql server. I was hoping to
alivate this via caching I was hoping do somthing like this.

public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = new Product("productXYZ",
(string)Application["connString"], Cache)
}
}



public class Product
{
protected string connString;
protected int productID;
...
protected decimal price;

public Product(string productName, string newConnString, Cache
currentCache)
{
connString = newConnString
if (currentCache["prodcut-" +productName] != null)
{
this = (Product)currentCache["prodcut-" +productName];
}
else
{
initProduct(productName);
}
}
}

But alas, the this keyword is read only. Is there a way to accomplish this?
I'd rather not have to go through everypage and put both a cache check and
cache insert statement. And it seems redundant to create a wrapper class to
handle the caching.

I suppose I could just cache the database results in the iniProduct
function, but somehow that seems less elegant. Any help?
 
S

sloan

Check my blog:
10/24/2005
http://spaces.msn.com/sholliday/

Your product class should be standalone, and not have "integrated" db calls.

You need something, I would call a Controller class.


class ProductController

public static GetProduct ( string prodid )
{

WebSessionDataStore wds = WebSessionDataStore.GetInstance();
if (null!=wds[prodid])
{
return (Product)wds[prodid];
}
else
{

// create a class, which reads the db... and populates a Product
//but don't put the code IN the Product class

Product p = Product ReadDataBaseAndCreateAProduct();
wds.Add(prodid , p);


}

private Product ReadDataBaseAndCreateAProduct(string prodid)
{

IDataReader idr = (some method to get a datareader on the database);

Product p = new Product();
p.Name = idr.GetString(0);

return p;

}



}


You got 2 things going on.
First, the "integrated data access" in your Product class....is deficient.
Make Product standalone, and create another class, which reads the
database....and populates a Product.

The Session Storer thing from my blog is a nice wrapper for handling caching
in an asp.net application.


...
 
B

Bruce Barker

switch to the factory model.


public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = Product.Create("productXYZ");
}
}

where Create is a static method that creates a class object. i would move
the cache control and connect string to the factory, i'd also use the
factory model for the settings, and call setup in application start.


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top