Skip to content

Commit 42b6c84

Browse files
committed
Fixed validation of ParentId for recursive Save()
When doing a recursive `Save()`, the `ParentID` attribute of each child topic was being validated to ensure that it had correctly been set to a positive number—i.e., that the parent's `Id` had been properly set from the database. There were two issues with this. First, we really only need to check that once, when the `topic.Id` is set, not for each child. Second, it was being checked _prior_ to the `ParentID` being set on the child. While I was at it, I set the `ParentID` based on `topic.Id` not `returnVal`; these will be the same, but the semantics are a bit clearer.
1 parent f92afc2 commit 42b6c84

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,11 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
796796

797797
topic.Id = returnVal;
798798

799+
Contract.Assume<InvalidOperationException>(
800+
topic.Id > 0,
801+
"The call to the CreateTopic stored procedure did not return the expected 'Id' parameter."
802+
);
803+
799804
/*----------------------------------------------------------------------------------------------------------------------
800805
| Add version to version history
801806
\---------------------------------------------------------------------------------------------------------------------*/
@@ -827,16 +832,13 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
827832
if (attributes != null) attributes.Dispose();
828833
}
829834

835+
830836
/*------------------------------------------------------------------------------------------------------------------------
831837
| Recurse
832838
\-----------------------------------------------------------------------------------------------------------------------*/
833839
if (isRecursive) {
834840
foreach (var childTopic in topic.Children) {
835-
Contract.Assume<InvalidOperationException>(
836-
childTopic.Attributes.GetInteger("ParentID", -1) > 0,
837-
"The call to the CreateTopic stored procedure did not return the expected 'ParentID' parameter."
838-
);
839-
childTopic.Attributes.SetValue("ParentID", returnVal.ToString(CultureInfo.InvariantCulture));
841+
childTopic.Attributes.SetValue("ParentID", topic.Id.ToString(CultureInfo.InvariantCulture));
840842
Save(childTopic, isRecursive, isDraft);
841843
}
842844
}

0 commit comments

Comments
 (0)