Skip to content

Commit 337f8ce

Browse files
committed
Implement the new TopicListDataTable class
In a previous commit (7a37dc9) we introduced a new `TopicListDataTable` which centralizes the definition of the `TopicList` table-valued type in the SQL Server database schema. In this commit, I implement that in the `SqlTopicRepository`, simplifying the code for `PersistRelations()`. As part of this, I moved the construction of the data table into the `for` loop so that we don't need to `Clear()` it each iteration. Not sure where this nets out in terms of performance, but it makes for cleaner and more intuitive code.
1 parent 7a37dc9 commit 337f8ce

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,6 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
657657
return "";
658658
}
659659
var command = (SqlCommand?)null;
660-
var targetIds = new DataTable();
661-
662-
targetIds.Columns.Add(
663-
new DataColumn("TopicID", typeof(int))
664-
);
665660

666661
try {
667662

@@ -670,6 +665,7 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
670665
\---------------------------------------------------------------------------------------------------------------------*/
671666
foreach (var key in topic.Relationships.Keys) {
672667

668+
var targetIds = new TopicListDataTable();
673669
var scope = topic.Relationships.GetTopics(key);
674670
var topicId = topic.Id.ToString(CultureInfo.InvariantCulture);
675671
var relatedTopics = scope.Where(t => t.Id > 0).Select<Topic, int>(m => m.Id);
@@ -679,9 +675,7 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
679675
};
680676

681677
foreach (var targetTopicId in relatedTopics) {
682-
var record = targetIds.NewRow();
683-
record["TopicID"] = targetTopicId;
684-
targetIds.Rows.Add(record);
678+
targetIds.AddRow(targetTopicId);
685679
}
686680

687681
// Add Parameters
@@ -691,8 +685,9 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
691685

692686
command.ExecuteNonQuery();
693687

694-
// Clear rows
695-
targetIds.Clear();
688+
targetIds.Dispose();
689+
690+
//Reset isDirty, assuming there aren't any unresolved references
696691
scope.IsDirty = relatedTopics.Count() < scope.Count;
697692

698693
}
@@ -714,7 +709,6 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
714709
\-----------------------------------------------------------------------------------------------------------------------*/
715710
finally {
716711
command?.Dispose();
717-
targetIds.Dispose();
718712
//Since the SQL connection is being passed in, do not close connection; this allows connection pooling.
719713
}
720714

0 commit comments

Comments
 (0)