I'm trying to come up with a generic model for associating some contextual data with an object in C#. I have built a caching system that can be described as follows...
Background Story -> The cache is a singleton implementation that provides "readonly" access to frequently requested information that is part of a custom CMS implementation that I use for various asp.net applications. I update the data via a desktop application I have written and the next time the web server loads the cache my changes are reflected to visitors. My cache adheres to the following...
Every object has a unique id
Any object can be associated with any other object by an id mapping defined in an association table
No matter how many associations to a particular object exist, only one instance of that object is loaded into the cache.
For example...Object A might associate to a collection of Object C's. Similarly Object B might also associate to a collection of Object C's. If one were to request an Object C with id 23 from an instance of Object A and then request an Object C with id 23 from Object B, they would get a handle to the same instance of Object C.
I now have some data to add into the picture but the data does not belong to Object A by itself and it does not belong to Object C by itself. The data is information specific to the association of Object A with Object C.
My First Idea: Keep the additional data separate from Object C since it doesn't actually belong to Object C by itself. Maintain this information within Object A and allow it to be looked up in a Dictionary. I don't like the way the data has to be accessed in this approach. I would rather have direct access to the additional data via Object C or a derived class for binding purposes and ease of use.
My Second Idea: Create a derived class from Object C (call it Object D) that includes the additional contextual data and provides properties for easy access. This addresses the binding and gives me the ease of use that I was looking for. My problem with this approach is that now my Object A is referring to a collection of Object D's and I am required to break my above model by duplicating the entirety of Object A's data just so that I can append some extra association information.
What I would really like is to continue having only one instance of Object C for a given id and append some contextual data and properties that can be easily accessed in the appropriate context. Can this be done? I'm also open for any other suggestions here! I want my solution to be generic and sound so I can forget about it and not have that constant itch to go back and find a better solution.