Skip to content

Commit 8aacc5a

Browse files
committed
Validate IsNew on Load(topic, version)
The `Load(topic, version)` is intended to load a previous version of the supplied topic. This is a friendly overload which calls `Load(topicId, version, topic)`. If `topic.IsNew`, however, then it will either fail—or, worse, potentially (attempt to) return that version of the root topic (if it exists). This is because an `IsNew` topic has an `Id` of -1, but some persistence stores treat requests to load a topic with an `Id` of -1 as a reference to the root. Regardless, this is expected to either fail or return incorrect results if the topic `IsNew`. Given that, it's better to validate this upfront and return a clear error message regarding the issue. This contributes to #77.
1 parent 646eb92 commit 8aacc5a

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

OnTopic/Repositories/TopicRepository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ protected ContentTypeDescriptorCollection SetContentTypeDescriptors(ContentTypeD
223223
/// <inheritdoc />
224224
public override Topic? Load(Topic topic, DateTime version) {
225225
Contract.Requires(topic, nameof(topic));
226+
Contract.Requires<ArgumentException>(
227+
!topic.IsNew,
228+
$"The version '{version}' of '{topic.GetUniqueKey()}' cannot be loaded. Topics must be saved in order to load " +
229+
$"previous versions."
230+
);
226231
return Load(topic.Id, version, topic);
227232
}
228233

0 commit comments

Comments
 (0)