Skip to content

Commit c3c01ef

Browse files
committed
Support saving arbitrary attributes
When an `ITopicRepository` implementation calls `GetAttributes()`, they will now receive arbitrary attributes—i.e., attributes that aren't specified in the `ContentTypeDescriptor` schema, but are defined in the `AttributeValueCollection`. This allows those arbitrary values to be programmatically defined and `Save()`d using the **OnTopic Library**. Previously, this functionality was exclusively available via the SQL database. Arbitrary attributes present some ambiguity. One source of ambiguity is whether they should be stored as _indexed_ or _extended_ attributes. Currently, most arbitrary attributes defined in the database are stored as _indexed_ attributes, which are easier to update and lookup via SQL. They also tend to be short, so this is an optimal format for them. That said, an arbitrary attribute _could_ be longer than 255—the limit for indexed attributes. Given this, the `GetAttributes()` will return an arbitrary attribute as _indexed_ if the length is 255 characters or less, and as _extended_ if it's greater than 255 characters. As part of this, I've also made the condtion for returning an attribute more concise. Finally, this includes a couple of unit tests for validating that arbitrary values are correctly being returned as either indexed or extended attributes via `GetAttributes()`. This supports Programmatic Support for Arbitrary Attributes (#19)
1 parent fb3366e commit c3c01ef

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

OnTopic.Tests/TopicRepositoryBaseTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,48 @@ public void GetAttributes_ExtendedAttributes_ReturnsExtendedAttributes() {
106106

107107
}
108108

109+
/*==========================================================================================================================
110+
| TEST: GET ATTRIBUTES: ARBITRARY ATTRIBUTE WITH SHORT VALUE: RETURNS AS INDEXED ATTRIBUTE
111+
\-------------------------------------------------------------------------------------------------------------------------*/
112+
/// <summary>
113+
/// Sets an arbitrary (unmatched) attribute on a <see cref="Topic"/> with a value shorter than 255 characters, then
114+
/// ensures that it is returned as an an <i>indexed</i> <see cref="AttributeValue"/> when calling <see
115+
/// cref="TopicRepositoryBase.GetAttributes(Topic, Boolean?, Boolean?)"/>.
116+
/// </summary>
117+
[TestMethod]
118+
public void GetAttributes_ArbitraryAttributeWithShortValue_ReturnsAsIndexedAttributes() {
119+
120+
var topic = TopicFactory.Create("Test", "ContentTypes");
121+
122+
topic.Attributes.SetValue("ArbitraryAttribute", "Value");
123+
124+
var attributes = _topicRepository.GetAttributesProxy(topic, false);
125+
126+
Assert.IsTrue(attributes.Any(a => a.Key.Equals("ArbitraryAttribute", StringComparison.InvariantCultureIgnoreCase)));
127+
128+
}
129+
130+
/*==========================================================================================================================
131+
| TEST: GET ATTRIBUTES: ARBITRARY ATTRIBUTE WITH LONG VALUE: RETURNS AS EXTENDED ATTRIBUTE
132+
\-------------------------------------------------------------------------------------------------------------------------*/
133+
/// <summary>
134+
/// Sets an arbitrary (unmatched) attribute on a <see cref="Topic"/> with a value longer than 255 characters, then
135+
/// ensures that it is returned as an an <see cref="AttributeDescriptor.IsExtendedAttribute"/> when calling <see
136+
/// cref="TopicRepositoryBase.GetAttributes(Topic, Boolean?, Boolean?)"/>.
137+
/// </summary>
138+
[TestMethod]
139+
public void GetAttributes_ArbitraryAttributeWithLongValue_ReturnsAsExtendedAttributes() {
140+
141+
var topic = TopicFactory.Create("Test", "ContentTypes");
142+
143+
topic.Attributes.SetValue("ArbitraryAttribute", new string('x', 256));
144+
145+
var attributes = _topicRepository.GetAttributesProxy(topic, true);
146+
147+
Assert.IsTrue(attributes.Any(a => a.Key.Equals("ArbitraryAttribute", StringComparison.InvariantCultureIgnoreCase)));
148+
149+
}
150+
109151
/*==========================================================================================================================
110152
| TEST: GET UNMATCHED ATTRIBUTES: RETURNS ATTRIBUTES
111153
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ protected IEnumerable<AttributeValue> GetAttributes(Topic topic, bool? isExtende
502502
attribute = contentType.AttributeDescriptors[key];
503503
}
504504

505-
if (attribute != null && (isExtendedAttribute == null || attribute.IsExtendedAttribute == isExtendedAttribute)) {
505+
//Add the attribute based on the isExtendedAttribute paramter. Add all parameters if isExtendedAttribute is null. Assume
506+
//an attribute is extended if the corresponding attribute descriptor cannot be located and the value is over 255
507+
//characters.
508+
if (isExtendedAttribute?.Equals(attribute?.IsExtendedAttribute?? attributeValue.Value?.Length > 255)?? true) {
506509
attributes.Add(attributeValue);
507510
}
508511

0 commit comments

Comments
 (0)