Skip to content

Commit 30bd3e5

Browse files
committed
Introduced GetUri() and SetUri() to AttributeCollection extensions
The `AttributeValueConverter` supports `Uri`, but for some reason the `AttributeCollection` extension methods didn't have a corresponding `GetUri()` or `SetUri()` method. This mitigates that issue.
1 parent 72ca032 commit 30bd3e5

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

OnTopic/Attributes/AttributeCollectionExtensions.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,43 @@ public static DateTime GetDateTime(
166166
)?? defaultValue;
167167
}
168168

169+
/*==========================================================================================================================
170+
| METHOD: GET URI
171+
\-------------------------------------------------------------------------------------------------------------------------*/
172+
/// <summary>
173+
/// Gets a named attribute from the Attributes dictionary with a specified default value, an optional setting for enabling
174+
/// of inheritance, and an optional setting for searching through base topics for values. Return as a URI.
175+
/// </summary>
176+
/// <param name="attributes">The instance of the <see cref="AttributeCollection"/> this extension is bound to.</param>
177+
/// <param name="name">The string identifier for the <see cref="AttributeRecord"/>.</param>
178+
/// <param name="defaultValue">A string value to which to fall back in the case the value is not found.</param>
179+
/// <param name="inheritFromParent">
180+
/// Boolean indicator nothing whether to search through the topic's parents in order to get the value.
181+
/// </param>
182+
/// <param name="inheritFromBase">
183+
/// Boolean indicator nothing whether to search through any of the topic's <see cref="Topic.BaseTopic"/>s in order to get
184+
/// the value.
185+
/// </param>
186+
/// <returns>The value for the attribute as a DateTime object.</returns>
187+
public static Uri? GetUri(
188+
this AttributeCollection attributes,
189+
string name,
190+
Uri? defaultValue = default,
191+
bool inheritFromParent = false,
192+
bool inheritFromBase = true
193+
) {
194+
Contract.Requires(attributes);
195+
Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(name), nameof(name));
196+
return AttributeValueConverter.Convert<Uri>(
197+
attributes.GetValue(
198+
name,
199+
null,
200+
inheritFromParent,
201+
inheritFromBase ? 5 : 0
202+
)
203+
)?? defaultValue;
204+
}
205+
169206
/*==========================================================================================================================
170207
| METHOD: SET BOOLEAN
171208
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -330,5 +367,47 @@ public static void SetDateTime(
330367
isDirty
331368
);
332369

370+
/*==========================================================================================================================
371+
| METHOD: SET URI
372+
\-------------------------------------------------------------------------------------------------------------------------*/
373+
/// <summary>
374+
/// Helper method that either adds a new <see cref="AttributeRecord"/> object or updates the value of an existing one,
375+
/// depending on whether that value already exists.
376+
/// </summary>
377+
/// <param name="attributes">The instance of the <see cref="AttributeCollection"/> this extension is bound to.</param>
378+
/// <param name="key">The string identifier for the <see cref="AttributeRecord"/>.</param>
379+
/// <param name="value">The <see cref="DateTime"/> value for the <see cref="AttributeRecord"/>.</param>
380+
/// <param name="isDirty">
381+
/// Specified whether the value should be marked as <see cref="TrackedRecord{T}.IsDirty"/>. By default, it will be marked
382+
/// as dirty if the value is new or has changed from a previous value. By setting this parameter, that behavior is
383+
/// overwritten to accept whatever value is submitted. This can be used, for instance, to prevent an update from being
384+
/// persisted to the data store on <see cref="ITopicRepository.Save(Topic, Boolean)"/>.
385+
/// </param>
386+
/// <requires
387+
/// description="The key must be specified for the AttributeRecord key/value pair."
388+
/// exception="T:System.ArgumentNullException">
389+
/// !String.IsNullOrWhiteSpace(key)
390+
/// </requires>
391+
/// <requires
392+
/// description="The value must be specified for the AttributeRecord key/value pair."
393+
/// exception="T:System.ArgumentNullException">
394+
/// !String.IsNullOrWhiteSpace(value)
395+
/// </requires>
396+
/// <requires
397+
/// description="The key should be an alphanumeric sequence; it should not contain spaces or symbols"
398+
/// exception="T:System.ArgumentException">
399+
/// !value.Contains(" ")
400+
/// </requires>
401+
public static void SetUri(
402+
this AttributeCollection attributes,
403+
string key,
404+
Uri value,
405+
bool? isDirty = null
406+
) => attributes?.SetValue(
407+
key,
408+
value?.ToString(),
409+
isDirty
410+
);
411+
333412
} //Class
334413
} //Namespace

0 commit comments

Comments
 (0)