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!