Quick BL/Caching question

Posts   
 
    
Posts: 497
Joined: 08-Apr-2004
# Posted on: 11-Apr-2005 16:13:20   

Hi,

I have a method, GetByID, that retrieves and returns an entity, by its ID. Simple. But....in my PL (ASP.NET), I store this entity in a session object because its used a lot.

So, I thought I should change my GetByID code so that it does this pseudo-code:


1. If HttpContext available
    1.1 If object in session exists with same ID as one requested
        1.1.1 Return object from session

Thus, no matter how many lazy calls are made to GetByID, I can be confident that there are no excessive DB hits. But, I don't like tieing the BL to the HttpContext too much, if for example I wrote another PL for it, I would want to do similar checks but obviously I can't work with HttpContext.

Is there a clever solution/pattern of sort for this, or should I go ahead and do what I suggested.....

Thanks all!

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 13-Apr-2005 17:53:58   

In your get by id method you could pass an abstract "cache manager object" to it. The abstract cache manager would have a method to return the cached object by id. If the object wasnt in the cache you could then invoke an internal get method on the cache manager which would fetch the data into cache and then return the results.

The trick is that the cache manager would be abstract and when you initialized a concrete instance of a cache manager, you would pass the cache context to the initialization routine, i.e. HttpContext or some other caching mechanism.

All of you objects that need to work with cached objects could simply work with an abstract cache manager and not need to worry about the caching mechanism.

This is just my theory anyway.