<p>So I just fixed a bug in a framework I'm developing. The pseudo-pseudocode looks like this:</p>

<pre><code>myoldObject = new MyObject { someValue = "old value" };
cache.Insert("myObjectKey", myoldObject);
myNewObject = cache.Get("myObjectKey");
myNewObject.someValue = "new value";
if(myObject.someValue != cache.Get("myObjectKey").someValue)
     myObject.SaveToDatabase();
</code></pre>

<p>So, essentially, I was getting an object from the cache, and then later on comparing the original object to the cached object to see if I need to save it to the database in case it's changed. The problem arose because the original object is a reference...so changing someValue also changed the referenced cached object, so it'd never save back to the database. I fixed it by cloning the object off of the cached version, severing the reference and allowing me to compare the new object against the cached one.</p>

<p>My question is: <strong>is there a better way to do this, some pattern, that you could recommend?</strong> I can't be the only person that's done this before :)</p>

[{'answer_id': 33507, 'author': 'Craig Walker', 'author_id': 3488, 'author_profile': 'https://Stackoverflow.com/users/3488', 'pm_score': 1, 'selected': False, 'text': "<p>I've done similar things, but I got around it by cloning too. The difference is that I had the cache do the cloning. When you put an object into the cache, the cache will clone the object first and store the cloned version (so you can mutate the original object without poisoning the cache). When you get an object from the cache, the cache returns a clone of the object instead of the stored object (again so that the caller can mutate the object without effecting the cached/canonical object).</p>\n\n<p>I think that this is perfectly acceptable as long as the data you're storing/duping is small. </p>\n"}, {'answer_id': 33795, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 6, 'selected': True, 'text': '<p>Dirty tracking is the normal way to handle this, I think. Something like:</p>\n\n<pre><code>class MyObject {\n  public string SomeValue { \n     get { return _someValue; }\n     set { \n       if (value != SomeValue) {\n          IsDirty = true;\n          _someValue = value;\n       }\n  }\n\n  public bool IsDirty {\n     get;\n     private set;\n  }\n\n  void SaveToDatabase() {\n     base.SaveToDatabase(); \n     IsDirty = false;\n  }\n}\n\nmyoldObject = new MyObject { someValue = "old value" };\ncache.Insert("myObjectKey", myoldObject);\nmyNewObject = cache.Get("myObjectKey");\nmyNewObject.someValue = "new value";\nif(myNewObject.IsDirty)\n   myNewObject.SaveToDatabase();\n</code></pre>\n'}, {'answer_id': 9415335, 'author': 'Contra', 'author_id': 112508, 'author_profile': 'https://Stackoverflow.com/users/112508', 'pm_score': 1, 'selected': False, 'text': '<p>A little improvement on Marks anwser when using linq:</p>\n\n<p>When using Linq, fetching entities from DB will mark every object as IsDirty. \nI made a workaround for this, by not setting IsDirty when the value is not set; for this instance: when null. For ints, I sat the orig-value to -1, and then checked for that. This will not work, however, if the saved value is the same as the uninitialized value (null in my example). </p>\n\n<pre><code>private string _name;\n[Column]\npublic string Name\n{\n    get { return _name; }\n    set\n    {\n        if (value != _name)\n        {\n            if (_name != null)\n            {\n                IsDirty = true;   \n            }\n            _name = value;\n        }\n    }\n}\n</code></pre>\n\n<p>Could probably be improved further by setting IsDirty after initialization somehow.</p>\n'}]
['https://Stackoverflow.com/questions/33469', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1212/']