diff --git a/src/GameCult.Caching/CultInspectorModel.cs b/src/GameCult.Caching/CultInspectorModel.cs index aaa7bbaf..6a86f5ee 100644 --- a/src/GameCult.Caching/CultInspectorModel.cs +++ b/src/GameCult.Caching/CultInspectorModel.cs @@ -252,7 +252,8 @@ namespace GameCult.Caching public object Document { get; } // The record being drawn, as stored when this edit began, for drawers that read the document they sit in. It is a - // second copy, made on first read: not the cached object, and not Document, which a lowering commits. Nothing + // second copy, decoded on first read from the bytes Document came from, so a cache change in between cannot split + // them: not the cached object, and not Document, which a lowering commits. Nothing // reads it back, so writing to it saves nothing and changes neither the edit nor the cache. public object Record => _record ??= _snapshot(); @@ -353,7 +354,8 @@ namespace GameCult.Caching { if (record == null) throw new ArgumentNullException(nameof(record)); var type = record.Descriptor.DocumentType; - return new CultInspectorEdit(record, Clone(record.Document, type), () => Clone(record.Document, type)); + var snapshot = _serialize(record.Document, type, type); + return new CultInspectorEdit(record, _deserialize(type, snapshot), () => _deserialize(type, snapshot)); } // Whether CreateDefault makes a value: a string, struct, list or dictionary, or a concrete class with a parameterless constructor. diff --git a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs index 1de5eaf8..7ce19824 100644 --- a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs +++ b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs @@ -199,6 +199,18 @@ namespace GameCult.Caching.Tests Assert.That(none, Does.Contain("cannot serialize")); } + [Test] + public async Task RecordIsTheStateTheEditBeganFrom() + { + using var cache = new CultCache(Registry); + await cache.UpsertAsync(typeof(InspectItem), new InspectItem { Name = "original" }); + var record = cache.AllStoredDocuments.Single(); + var edit = Model().BeginEdit(record); + ((InspectItem)record.Document).Name = "changed in place before Record was read"; + + Assert.That(((InspectItem)edit.Record).Name, Is.EqualTo(((InspectItem)edit.Document).Name).And.EqualTo("original")); + } + [Test] public async Task RefusedCommitLeavesTheCachedDocumentUnchanged() {