2

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...

  1. Every object has a unique id

  2. Any object can be associated with any other object by an id mapping defined in an association table

  3. 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.

  1. 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.

  2. 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.

4

2 回答 2

0

Objec C 实例应该在那里保存自己的数据。当然,您可以通过从缓存中检索到的对象(通过对象 A 或对象 B)将“路径”存储在其中,但是将此类信息存储在对象中会修改对象,因此如果要检索它通过另一个“路径”,信息将被覆盖。

一个解决方案可能是每次从缓存中检索对象 C 时克隆它并将您的“路径”存储在其中,或者如果它来自对象 A,则存储您的额外数据?

这样,缓存内的所有对象都按请求共享,但一旦在缓存外独立。

于 2013-04-29T14:01:26.457 回答
0

我最终选择了我的第二个想法。对象 D 变成了一种特殊类型的对象,它有自己的属性/成员/方法,但也有一个返回对象 C 的属性。对象 D 必须实现一个特定的接口才能被我的缓存算法识别。我更新了我的缓存系统,以确保如果该属性返回的对象 C 已经被加载,则返回那个对象,而不是从数据库加载一个额外的副本。

于 2013-12-05T20:23:53.257 回答