Skip to content

Commit 31ee953

Browse files
committed
Introduced specialized AttributeDictionary
Ideally, models will optionally accept a dictionary of attributes so that scalar properties can be quickly and easily assigned without relying on reflection, in order to improve performance. The `AttributeDictionary` class will not only provide a friendlier signature than e.g. `IDictionary<string, string>`, but will also provide a friendlier `GetValue()` method for getting a value if there's a match, and otherwise returning `null`. This will aid in supporting Feature #99.
1 parent ee312b2 commit 31ee953

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
using OnTopic.Mapping;
7+
8+
namespace OnTopic.Attributes {
9+
10+
/*============================================================================================================================
11+
| CLASS: ATTRIBUTE DICTIONARY
12+
\---------------------------------------------------------------------------------------------------------------------------*/
13+
/// <summary>
14+
/// Provides a light-weight dictionary of attribute values.
15+
/// </summary>
16+
/// <remarks>
17+
/// The <see cref="AttributeDictionary"/> is used by the <see cref="TopicMappingService"/> to support self-constructed
18+
/// models that don't require the use of the <see cref="TopicMappingService"/> to set the values of scalar properties. Any
19+
/// model that has a primary constructor accepting a single <see cref="AttributeDictionary"/> will be initialized using a
20+
/// <see cref="AttributeDictionary"/> containing all attributes from not only the current <see cref="Topic"/>, but also any
21+
/// <see cref="Topic.BaseTopic"/>s it references.
22+
/// </remarks>
23+
public class AttributeDictionary: Dictionary<string, string?> {
24+
25+
/*==========================================================================================================================
26+
| CONSTRUCTOR
27+
\-------------------------------------------------------------------------------------------------------------------------*/
28+
/// <inheritdoc/>
29+
public AttributeDictionary(): base(StringComparer.OrdinalIgnoreCase) { }
30+
31+
/*==========================================================================================================================
32+
| METHOD: GET VALUE
33+
\-------------------------------------------------------------------------------------------------------------------------*/
34+
/// <summary>
35+
/// Gets an attribute value as a string from the <see cref="AttributeDictionary"/> based on the <paramref name="
36+
/// attributeKey"/>.
37+
/// </summary>
38+
/// <param name="attributeKey">The key of the attribute to retrieve.</param>
39+
/// <returns></returns>
40+
public string? GetValue(string attributeKey) {
41+
Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(attributeKey), nameof(attributeKey));
42+
TryGetValue(attributeKey, out var value);
43+
return String.IsNullOrWhiteSpace(value)? null : value;
44+
}
45+
46+
47+
} //Class
48+
} //Namespace

0 commit comments

Comments
 (0)