Skip to content

Commit 72fb0b5

Browse files
committed
Share single version across recursive Save()
Previously, `version` was defined on each iteration of `Save()`. This means if a recursive `Save()` occurred, each topic would have a _slightly_ different `Version` in the database. This didn't hurt anything, but could make it difficult to track what `Save()`s occurred as part of a single request when debugging. More importantly, with the unresolved reference handling (0c860ae), this meant that the same topic could end up with two different versions generated for the same `Save()`. This remedies both issues by centralizing the definition via the public `Save()` entry point.
1 parent 7bea7d3 commit 72fb0b5

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.Data.SqlClient;
1515
using OnTopic.Data.Sql.Models;
1616
using OnTopic.Internal.Diagnostics;
17-
using OnTopic.Metadata;
1817
using OnTopic.Repositories;
1918

2019
namespace OnTopic.Data.Sql {
@@ -269,6 +268,7 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
269268
/*------------------------------------------------------------------------------------------------------------------------
270269
| Establish dependencies
271270
\-----------------------------------------------------------------------------------------------------------------------*/
271+
var version = new SqlDateTime(DateTime.Now);
272272
var unresolvedTopics = new List<Topic>();
273273
var connection = new SqlConnection(_connectionString);
274274

@@ -277,13 +277,13 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
277277
/*------------------------------------------------------------------------------------------------------------------------
278278
| Handle first pass
279279
\-----------------------------------------------------------------------------------------------------------------------*/
280-
var topicId = Save(topic, isRecursive, isDraft, connection, unresolvedTopics);
280+
var topicId = Save(topic, isRecursive, isDraft, connection, unresolvedTopics, version);
281281

282282
/*------------------------------------------------------------------------------------------------------------------------
283283
| Attempt to resolve outstanding relationships
284284
\-----------------------------------------------------------------------------------------------------------------------*/
285285
foreach (var unresolvedTopic in unresolvedTopics) {
286-
Save(unresolvedTopic, false, isDraft, connection, new List<Topic>());
286+
Save(unresolvedTopic, false, isDraft, connection, new List<Topic>(), version);
287287
}
288288

289289
/*------------------------------------------------------------------------------------------------------------------------
@@ -325,7 +325,8 @@ private int Save(
325325
bool isRecursive,
326326
bool isDraft,
327327
SqlConnection connection,
328-
List<Topic> unresolvedRelationships
328+
List<Topic> unresolvedRelationships,
329+
SqlDateTime version
329330
) {
330331

331332
/*------------------------------------------------------------------------------------------------------------------------
@@ -337,7 +338,6 @@ List<Topic> unresolvedRelationships
337338
| Define variables
338339
\-----------------------------------------------------------------------------------------------------------------------*/
339340
var isNew = topic.Id == -1;
340-
var version = new SqlDateTime(DateTime.Now);
341341
var areReferencesResolved = true;
342342
var areRelationshipsDirty = topic.Relationships.IsDirty();
343343
var areAttributesDirty = topic.Attributes.IsDirty(excludeLastModified: true);
@@ -468,7 +468,9 @@ List<Topic> unresolvedRelationships
468468
PersistRelations(topic, connection);
469469
}
470470

471-
topic.VersionHistory.Insert(0, version.Value);
471+
if (!topic.VersionHistory.Contains(version.Value)) {
472+
topic.VersionHistory.Insert(0, version.Value);
473+
}
472474

473475
}
474476

@@ -504,7 +506,7 @@ void recurse() {
504506
if (isRecursive) {
505507
foreach (var childTopic in topic.Children) {
506508
childTopic.Attributes.SetValue("ParentID", topic.Id.ToString(CultureInfo.InvariantCulture));
507-
Save(childTopic, isRecursive, isDraft, connection, unresolvedRelationships);
509+
Save(childTopic, isRecursive, isDraft, connection, unresolvedRelationships, version);
508510
}
509511
}
510512
}

0 commit comments

Comments
 (0)