help needed! C# object cache class..


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: help needed! C# object cache class..

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    18

    Thumbs up 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!

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    http://msdn2.microsoft.com/en-US/library/ms173116.aspx

    If your classes contain so many members (perhaps including references to other objects) that it's impractical to copy them manually, you might try serializing returnedObject and deserializing it to "this." Serialization, however, employs reflection and is therefore expensive; it may negate the advantage of caching the object in the first place.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by Phil Weber
    http://msdn2.microsoft.com/en-US/library/ms173116.aspx

    If your classes contain so many members (perhaps including references to other objects) that it's impractical to copy them manually, you might try serializing returnedObject and deserializing it to "this." Serialization, however, employs reflection and is therefore expensive; it may negate the advantage of caching the object in the first place.
    Cheers Phil,
    From what you've said i've decided to populate the objects members manually, I was wondering what your take on my architecture was however, is it acceptable to put database calls in constructors to fetch information for that object or would you normally create the object with all of the parameters after making the database calls seperatley to get that objects members..

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I would probably create a couple of layers: a "pure" class definition which knows nothing about the data source and whose constructor accepts initialization parameters; and a wrapper class that queries the data source and passes initialization values to the inner class. I would keep both of the above separate from the presentation layer.

    With that architecture, if the data source changes, you don't need to modify the UI or the class definition.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 05:12 AM
  2. Convert Class Object to JSON string?
    By quantass in forum ASP.NET
    Replies: 2
    Last Post: 06-13-2012, 05:52 AM
  3. Hierarchy of Shape
    By Tmcclain in forum Java
    Replies: 1
    Last Post: 12-17-2006, 01:31 AM
  4. Scope of the Cache Object
    By michael in forum ASP.NET
    Replies: 3
    Last Post: 04-12-2003, 11:49 AM
  5. Object REferences from within a class.
    By Tyler W in forum .NET
    Replies: 1
    Last Post: 11-03-2002, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center