|
-
help needed! C# object cache class..
Hi everyone,
First of all, I'm not even sure if I'm doing this the right way or if i have started coding this in a fundamentally flawed way but...
I have a large number of classes... in my application opening one screen may require the creation of several objects which may then create their own supporting objects, in the constructor of each is the code to fetch the data for that object from the database, is this a standard approach or horribly wrong?
My problem is the large number of queries generated so I decided to create a static class to enable reuse of objects...
upon hitting the constructor for a cached object, the static cache is called first to check if that object is in the cache's hashtable, if it is the object is returned, if not the constructor is allowed to continue and populate the object, at the end, the cache is updated with the new object..
Now, my problem is that in the constructor, if the object is found in the cache what is the best way to populate that objects fields with fields from the object returned from the cache...
I just found I couldn't say " this = returnedObject " as <this> is readonly.
hope this makes sense!
it's fairly basic here is the code for this...
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ObjectCache
{
public static class ObjectCache
{
//function of this is to cache objects to stop them being recreated from the database
private static Hashtable ObjectCacheTable = new Hashtable();
public static CacheReturnValue checkCache(string type, string ID)
{
String paramString = type + ":" + ID;
if (ObjectCacheTable[paramString] == null)
{
CacheReturnValue rtn = new CacheReturnValue(false, null, null);
return rtn;
}
else
{
CacheReturnValue rtn = new CacheReturnValue(true, ObjectCacheTable[paramString], type);
return rtn;
}
}
public static void registerNewObject(object obj, string type, string ID)
{
String paramString = type + ":" + ID;
ObjectCacheTable.Add(paramString, obj);
}
public static void unregisterObject(string type, string ID)
{
String paramString = type + ":" + ID;
ObjectCacheTable.Remove(paramString);
}
}
public class CacheReturnValue
{
bool objectFound;
object returnedObject;
string objectType;
public CacheReturnValue(bool objectFound, object returnedObject, string objectType)
{
this.objectFound = objectFound;
this.returnedObject = returnedObject;
this.objectType = objectType;
}
public bool isObjectFound()
{
return objectFound;
}
public object getObject()
{
return returnedObject;
}
}
}
Any help/insight/ideas would be greatly appreciated
Thanks!
Similar Threads
-
Replies: 26
Last Post: 12-01-2012, 04:12 AM
-
By quantass in forum ASP.NET
Replies: 2
Last Post: 06-13-2012, 04:52 AM
-
By Tmcclain in forum Java
Replies: 1
Last Post: 12-17-2006, 12:31 AM
-
By michael in forum ASP.NET
Replies: 3
Last Post: 04-12-2003, 10:49 AM
-
Replies: 1
Last Post: 11-03-2002, 08:30 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks