Skip to content

Commit 1e9152a

Browse files
committed
Reevaluate DerivedTopic on Save()
If a `DerivedTopic` is set prior to it being saved, the `DerivedTopic.Id` won't be persisted as a `TopicID` attribute in the `Topic.Attributes` collection. This is by design to prevent an invalid relationship from being persisted to the underlying data store (a3b6da1). The problem with this, however, is that the `Topic` has no (current) way of knowing when the target `DerivedTopic` has been saved, and thus its underlying `TopicID` attribute will remain unset even after the `DerivedTopic` has received a valid `Topic.Id`. To mitigate this, the `TopicRepositoryBase.Save()` method now sets `DerivedTopic` to itself. If the `Id` has changed—i.e., if the `Id` has been set—then that will cause it to be persisted to the `Topic.Attributes` collection and subsequently persisted to the underlying data storage. This fixes a bug that occurred when recursively saving in-memory topic graphs, as otherwise `DerivedTopic`s would only be persisted if either a) they were saved before the source class, or b) the `DerivedTopic` relationship was reestablished before a subsequent `Save()`. This is exactly how e.g. the **OnTopic Editor** works with the **OnTopic Data Transfer** library—but we shouldn't rely on implementors knowing this. By baking this into `Save()`, we help prevent unintuitive results when working with these scenarios.
1 parent a3b6da1 commit 1e9152a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ public virtual int Save([ValidatedNotNull, NotNull]Topic topic, bool isRecursive
304304
contentTypeDescriptors.Add((ContentTypeDescriptor)topic);
305305
}
306306

307+
/*------------------------------------------------------------------------------------------------------------------------
308+
| Ensure derived topic is set
309+
>-------------------------------------------------------------------------------------------------------------------------
310+
| ### HACK JJC20200523: If a derived topic is linked but hasn't been saved yet, then it should not be persisted to the
311+
| repository, as its topic.Id will be -1. If a derived topic is saved after the relationship has been established,
312+
| however, there isn't currently a way to detect that event and subsequently update the TopicId attribute. To mitigate
313+
| that, we simply set the derived topic to itself before Save(); if it has been saved in the interim, then the topic.Id
314+
| will be set; if not, the topic.Id will remain -1.
315+
\-----------------------------------------------------------------------------------------------------------------------*/
316+
topic.DerivedTopic = topic.DerivedTopic;
317+
307318
/*------------------------------------------------------------------------------------------------------------------------
308319
| Trigger event
309320
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)