Skip to content

Commit 3fd9720

Browse files
committed
Provide escape at top of Save() if !isDirty
By moving all of the key variables and logic for `isDirty` to the top of `Save()` (baaaf4d ), we can now escape the `Save()` method early if nothing has actually changed. This prevents the need to populate the indexed attributes `DataTable`, extended attributes XML, and command parameters, even though none of those would be used. (Alternatively, it saves from wrapping the bulk of the method in a conditional block, which I wanted to avoid.) To facilitate this, the closing logic of iterating over child topics as part of the `isRecursive` support is centralized into a new local `recurse()` method, so that we don't need to repeat that logic.
1 parent baaaf4d commit 3fd9720

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ List<Topic> unresolvedRelationships
362362
indexedAttributeList.Any() ||
363363
extendedAttributeList.Any(a => a.IsExtendedAttribute == false);
364364

365+
/*------------------------------------------------------------------------------------------------------------------------
366+
| Bypass is not dirty
367+
\-----------------------------------------------------------------------------------------------------------------------*/
368+
if (!isDirty) {
369+
recurse();
370+
return topic.Id;
371+
}
372+
365373
/*------------------------------------------------------------------------------------------------------------------------
366374
| Establish attribute containers with schema
367375
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -472,10 +480,9 @@ void addUnmatchedAttribute(string key) {
472480
\-----------------------------------------------------------------------------------------------------------------------*/
473481
try {
474482

475-
if (isDirty) {
476-
command.ExecuteNonQuery();
477-
topic.Id = command.GetReturnCode();
478-
}
483+
command.ExecuteNonQuery();
484+
485+
topic.Id = command.GetReturnCode();
479486

480487
Contract.Assume<InvalidOperationException>(
481488
topic.Id > 0,
@@ -510,19 +517,22 @@ void addUnmatchedAttribute(string key) {
510517
}
511518

512519
/*------------------------------------------------------------------------------------------------------------------------
513-
| Recurse
520+
| Return value
514521
\-----------------------------------------------------------------------------------------------------------------------*/
515-
if (isRecursive) {
516-
foreach (var childTopic in topic.Children) {
517-
childTopic.Attributes.SetValue("ParentID", topic.Id.ToString(CultureInfo.InvariantCulture));
518-
Save(childTopic, isRecursive, isDraft, connection, unresolvedRelationships);
519-
}
520-
}
522+
recurse();
523+
return topic.Id;
521524

522525
/*------------------------------------------------------------------------------------------------------------------------
523-
| Return value
526+
| Recurse
524527
\-----------------------------------------------------------------------------------------------------------------------*/
525-
return topic.Id;
528+
void recurse() {
529+
if (isRecursive) {
530+
foreach (var childTopic in topic.Children) {
531+
childTopic.Attributes.SetValue("ParentID", topic.Id.ToString(CultureInfo.InvariantCulture));
532+
Save(childTopic, isRecursive, isDraft, connection, unresolvedRelationships);
533+
}
534+
}
535+
}
526536

527537
}
528538

0 commit comments

Comments
 (0)