Skip to content

Commit 4da0d43

Browse files
committed
Validate IsNew of Parent on Save()
The `Save()` method is obviously equipped to deal with `IsNew` topics. But it expects that the _parent_ of the current topic is saved. If not, it won't be able to save the child topic—or, if it were, it would be orphaned in the persistence store's hierarchy. Given that, I've added code to validate that the `Parent` is either `null` or `!IsNew`. (Note: This doesn't affect recursive saves because they will save the parent before recursing through its children.) This contributes to #77.
1 parent 8aacc5a commit 4da0d43

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

OnTopic/Repositories/TopicRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ public override void Rollback([ValidatedNotNull]Topic topic, DateTime version) {
265265
/// <inheritdoc />
266266
public override sealed void Save([ValidatedNotNull] Topic topic, bool isRecursive = false) {
267267

268+
/*------------------------------------------------------------------------------------------------------------------------
269+
| Establish parameters
270+
\-----------------------------------------------------------------------------------------------------------------------*/
271+
Contract.Requires(topic, nameof(topic));
272+
Contract.Requires<ArgumentException>(
273+
topic.Parent is null || !topic.Parent.IsNew,
274+
$"The parent of '{topic.GetUniqueKey()}' is not saved. Topics can only be saved once their parent is saved."
275+
);
276+
268277
/*------------------------------------------------------------------------------------------------------------------------
269278
| Establish dependencies
270279
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)