Caching objects

Discussion in 'ASP .Net' started by Joel Barsotti, Jan 27, 2006.

  1. 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?
     
    Joel Barsotti, Jan 27, 2006
    #1
    1. Advertising

  2. Joel Barsotti

    sloan Guest

    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.


    ...



    "Joel Barsotti" <> wrote in message
    news:%...
    > 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?
    >
    >
     
    sloan, Jan 27, 2006
    #2
    1. Advertising

  3. Joel Barsotti

    Bruce Barker Guest

    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)


    "Joel Barsotti" <> wrote in message
    news:%...
    > 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?
    >
    >
     
    Bruce Barker, Jan 28, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hypo
    Replies:
    6
    Views:
    454
  2. Troy Simpson

    Fragment Caching inside page caching?

    Troy Simpson, Jan 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    827
    Troy Simpson
    Jan 19, 2004
  3. 7stud
    Replies:
    11
    Views:
    729
    Dennis Lee Bieber
    Mar 20, 2007
  4. News123
    Replies:
    7
    Views:
    393
    Dave Angel
    Jun 29, 2009
  5. JimLad
    Replies:
    3
    Views:
    941
    JimLad
    Jan 21, 2010
Loading...

Share This Page