Skip to content

Commit 272dabf

Browse files
committed
Implement the new AttributeValuesDataTable class
In a previous commit (313fe71) we introduced a new `AttributeValuesDataTable` which centralizes the definition of the `AttributeValues` table-valued type in the SQL Server database schema. In this commit, I implement that in the `SqlTopicRepository`, simplifying the code for `Save()`. As part of this, renamed `attributes` to `attributeValues` for consistency with the data table and type name.
1 parent 313fe71 commit 272dabf

1 file changed

Lines changed: 10 additions & 35 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Linq;
1313
using System.Text;
1414
using Microsoft.Data.SqlClient;
15+
using OnTopic.Data.Sql.Models;
1516
using OnTopic.Internal.Diagnostics;
1617
using OnTopic.Metadata;
1718
using OnTopic.Repositories;
@@ -370,40 +371,21 @@ List<Topic> unresolvedRelationships
370371
return topic.Id;
371372
}
372373

373-
/*------------------------------------------------------------------------------------------------------------------------
374-
| Establish attribute containers with schema
375-
\-----------------------------------------------------------------------------------------------------------------------*/
376-
var extendedAttributes = new StringBuilder();
377-
var attributes = new DataTable();
378-
379-
attributes.Columns.Add(
380-
new DataColumn("AttributeKey") {
381-
MaxLength = 128
382-
}
383-
);
384-
attributes.Columns.Add(
385-
new DataColumn("AttributeValue") {
386-
MaxLength = 255
387-
}
388-
);
389-
390374
/*------------------------------------------------------------------------------------------------------------------------
391375
| Add indexed attributes that are dirty
392376
\-----------------------------------------------------------------------------------------------------------------------*/
393-
foreach (var attributeValue in indexedAttributeList) {
377+
var attributeValues = new AttributeValuesDataTable();
394378

395-
var record = attributes.NewRow();
396-
record["AttributeKey"] = attributeValue.Key;
397-
record["AttributeValue"]= attributeValue.Value;
379+
foreach (var attributeValue in indexedAttributeList) {
380+
attributeValues.AddRow(attributeValue.Key, attributeValue.Value);
398381
attributeValue.IsDirty = false;
399-
400-
attributes.Rows.Add(record);
401-
402382
}
403383

404384
/*------------------------------------------------------------------------------------------------------------------------
405385
| Add extended attributes
406386
\-----------------------------------------------------------------------------------------------------------------------*/
387+
var extendedAttributes = new StringBuilder();
388+
407389
extendedAttributes.Append("<attributes>");
408390

409391
foreach (var attributeValue in extendedAttributeList) {
@@ -418,7 +400,7 @@ List<Topic> unresolvedRelationships
418400
//extended attribute, as it persists that version history, while removing ambiguity over which record is authoritative.
419401
//This is also useful for supporting arbitrary attribute values, since they may be moved from indexed to extended
420402
//attributes if their length exceeds 255.
421-
addUnmatchedAttribute(attributeValue.Key);
403+
attributeValues.AddRow(attributeValue.Key);
422404

423405
}
424406

@@ -430,14 +412,7 @@ List<Topic> unresolvedRelationships
430412

431413
//Loop through the content type's supported attributes and add attribute to null attributes if topic does not contain it
432414
foreach (var attribute in GetUnmatchedAttributes(topic)) {
433-
addUnmatchedAttribute(attribute.Key);
434-
}
435-
436-
void addUnmatchedAttribute(string key) {
437-
var record = attributes.NewRow();
438-
record["AttributeKey"] = key;
439-
record["AttributeValue"]= null;
440-
attributes.Rows.Add(record);
415+
attributeValues.AddRow(attribute.Key);
441416
}
442417

443418
/*------------------------------------------------------------------------------------------------------------------------
@@ -472,7 +447,7 @@ void addUnmatchedAttribute(string key) {
472447
}
473448
command.AddParameter("Version", version.Value);
474449
command.AddParameter("ExtendedAttributes", extendedAttributes);
475-
command.Parameters.AddWithValue("@Attributes", attributes);
450+
command.Parameters.AddWithValue("@Attributes", attributeValues);
476451
command.AddOutputParameter();
477452

478453
/*------------------------------------------------------------------------------------------------------------------------
@@ -513,7 +488,7 @@ void addUnmatchedAttribute(string key) {
513488
\-----------------------------------------------------------------------------------------------------------------------*/
514489
finally {
515490
command?.Dispose();
516-
attributes.Dispose();
491+
attributeValues.Dispose();
517492
}
518493

519494
/*------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)