Skip to content

Commit 7eb55f4

Browse files
committed
Introduced new GetCompositeAttributeKey() method
With the `attributePrefix` removed from the `ItemConfiguration`, the onus is on the caller to ensure that the `attributePrefix` is accounted for, if relevant. To simplify this process, I've added a simple convenience method for concatenating the full attribute key. This will be implemented in subsequent versions of `TopicMappingService` and `ReverseTopicMappingService`. I've also updated all calls to `AttributeKey` to now call the new `GetCompositeAttributeKey()`. This effectively reinstates support for `attributePrefix`, which was broken when `ItemConfiguration` was moved to `ItemMetadata` (74cb817, 84735fb) and the mapping services were updated to rely on `ItemMetadata` instead of creating their own instances of `ItemConfiguration` (fe39302). All unit tests related to `[MapToParent]` now pass again.
1 parent fe39302 commit 7eb55f4

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

OnTopic/Mapping/Internal/ItemConfiguration.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ internal ItemConfiguration(ItemMetadata itemMetadata) {
422422
/// </remarks>
423423
internal Dictionary<string, string> AttributeFilters { get; }
424424

425+
/*==========================================================================================================================
426+
| METHOD: GET COMPOSITE ATTRIBUTE KEY
427+
\-------------------------------------------------------------------------------------------------------------------------*/
428+
/// <summary>
429+
/// Retrieves the current <see cref="AttributeKey"/>, and, optionally, the supplied <paramref name="attributePrefix"/>.
430+
/// </summary>
431+
/// <param name="attributePrefix">
432+
/// The current mapping operation's attribute prefix to be prepended to the <see cref="AttributeKey"/>.
433+
/// </param>
434+
/// <returns>
435+
/// The composite attribute key, based on <see cref="AttributeKey"/> and, optionally, the supplied <paramref name="
436+
/// attributePrefix"/>.
437+
/// </returns>
438+
internal string GetCompositeAttributeKey(string? attributePrefix) => $"{attributePrefix}{AttributeKey}";
439+
425440
/*==========================================================================================================================
426441
| METHOD: SATISFIES ATTRIBUTE FILTERS
427442
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Mapping/Reverse/BindingModelValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ static internal void ValidateProperty(
147147
/*------------------------------------------------------------------------------------------------------------------------
148148
| Define variables
149149
\-----------------------------------------------------------------------------------------------------------------------*/
150-
var compositeAttributeKey = configuration.AttributeKey;
151150
var configuration = propertyAccessor.Configuration;
151+
var compositeAttributeKey = configuration.GetCompositeAttributeKey(attributePrefix);
152152
var attributeDescriptor = contentTypeDescriptor.AttributeDescriptors.GetValue(compositeAttributeKey);
153153
var childCollections = new[] { CollectionType.Children, CollectionType.NestedTopics };
154154
var relationships = new[] { CollectionType.Relationship, CollectionType.IncomingRelationship };
@@ -208,7 +208,7 @@ static internal void ValidateProperty(
208208
/*------------------------------------------------------------------------------------------------------------------------
209209
| Handle parent
210210
\-----------------------------------------------------------------------------------------------------------------------*/
211-
if (configuration.AttributeKey is "Parent") {
211+
if (configuration.GetCompositeAttributeKey(attributePrefix) is "Parent") {
212212
throw new MappingModelValidationException(
213213
$"The {nameof(ReverseTopicMappingService)} does not support mapping Parent topics. This property should be " +
214214
$"removed from the binding model, or otherwise decorated with the {nameof(DisableMappingAttribute)} to prevent " +

OnTopic/Mapping/Reverse/ReverseTopicMappingService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private async Task SetPropertyAsync(
241241
\-----------------------------------------------------------------------------------------------------------------------*/
242242
var configuration = memberAccessor.Configuration;
243243
var contentTypeDescriptor = _contentTypeDescriptors.GetValue(target.ContentType);
244-
var compositeAttributeKey = configuration.AttributeKey;
244+
var compositeAttributeKey = configuration.GetCompositeAttributeKey(attributePrefix);
245245

246246
Contract.Assume(contentTypeDescriptor, nameof(contentTypeDescriptor));
247247

@@ -278,7 +278,7 @@ await MapAsync(
278278

279279
if (attributeType is null) {
280280
throw new MappingModelValidationException(
281-
$"The attribute '{configuration.AttributeKey}' mapped by the {source.GetType()} could not be found on the " +
281+
$"The attribute '{configuration.GetCompositeAttributeKey(attributePrefix)}' mapped by the {source.GetType()} could not be found on the " +
282282
$"'{contentTypeDescriptor.Key}' content type.");
283283
}
284284

@@ -362,7 +362,7 @@ private static void SetScalarValue(
362362
/*------------------------------------------------------------------------------------------------------------------------
363363
| Set the value (to null, if appropriate)
364364
\-----------------------------------------------------------------------------------------------------------------------*/
365-
target.Attributes.SetValue(configuration.AttributeKey, attributeValue);
365+
target.Attributes.SetValue(configuration.GetCompositeAttributeKey(attributePrefix), attributeValue);
366366

367367
}
368368

@@ -403,7 +403,7 @@ private void SetRelationships(
403403
/*------------------------------------------------------------------------------------------------------------------------
404404
| Clear existing relationships
405405
\-----------------------------------------------------------------------------------------------------------------------*/
406-
target.Relationships.Clear(configuration.AttributeKey);
406+
target.Relationships.Clear(configuration.GetCompositeAttributeKey(attributePrefix));
407407

408408
/*------------------------------------------------------------------------------------------------------------------------
409409
| Set relationships for each
@@ -416,7 +416,7 @@ private void SetRelationships(
416416
$"located in the repository."
417417
);
418418
}
419-
target.Relationships.SetValue(configuration.AttributeKey, targetTopic);
419+
target.Relationships.SetValue(configuration.GetCompositeAttributeKey(attributePrefix), targetTopic);
420420
}
421421

422422
}
@@ -454,9 +454,9 @@ private async Task SetNestedTopicsAsync(
454454
/*------------------------------------------------------------------------------------------------------------------------
455455
| Establish target collection to store mapped topics
456456
\-----------------------------------------------------------------------------------------------------------------------*/
457-
var container = target.Children.GetValue(configuration.AttributeKey);
457+
var container = target.Children.GetValue(configuration.GetCompositeAttributeKey(attributePrefix));
458458
if (container is null) {
459-
container = TopicFactory.Create(configuration.AttributeKey, "List", target);
459+
container = TopicFactory.Create(configuration.GetCompositeAttributeKey(attributePrefix), "List", target);
460460
container.IsHidden = true;
461461
}
462462

@@ -525,11 +525,11 @@ private void SetReference(
525525
/*------------------------------------------------------------------------------------------------------------------------
526526
| Set target attribute
527527
\-----------------------------------------------------------------------------------------------------------------------*/
528-
if (configuration.AttributeKey.EndsWith("Id", StringComparison.Ordinal)) {
529-
target.Attributes.SetValue(configuration.AttributeKey, topicReference?.Id.ToString(CultureInfo.InvariantCulture));
528+
if (configuration.GetCompositeAttributeKey(attributePrefix).EndsWith("Id", StringComparison.Ordinal)) {
529+
target.Attributes.SetValue(configuration.GetCompositeAttributeKey(attributePrefix), topicReference?.Id.ToString(CultureInfo.InvariantCulture));
530530
}
531531
else {
532-
target.References.SetValue(configuration.AttributeKey, topicReference);
532+
target.References.SetValue(configuration.GetCompositeAttributeKey(attributePrefix), topicReference);
533533
}
534534

535535
}

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ await MapAsync(
567567
else if (itemMetadata.IsList) {
568568
return null;
569569
}
570-
else if (configuration.AttributeKey is "Parent") {
570+
else if (configuration.GetCompositeAttributeKey(attributePrefix) is "Parent") {
571571
if (associations.HasFlag(AssociationTypes.Parents) && source.Parent is not null) {
572572
value = await GetTopicReferenceAsync(source.Parent, targetType, itemMetadata, cache).ConfigureAwait(false);
573573
}
@@ -593,17 +593,17 @@ await MapAsync(
593593
Topic? getTopicReference() {
594594

595595
// Check for standard topic reference
596-
var topicReference = source.References.GetValue(configuration.AttributeKey);
596+
var topicReference = source.References.GetValue(configuration.GetCompositeAttributeKey(attributePrefix));
597597
if (topicReference is not null) {
598598
return topicReference;
599599
}
600600

601601
int topicReferenceId;
602-
if (configuration.AttributeKey.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) {
603-
topicReferenceId = source.Attributes.GetInteger(configuration.AttributeKey, 0);
602+
if (configuration.GetCompositeAttributeKey(attributePrefix).EndsWith("Id", StringComparison.OrdinalIgnoreCase)) {
603+
topicReferenceId = source.Attributes.GetInteger(configuration.GetCompositeAttributeKey(attributePrefix), 0);
604604
}
605605
else {
606-
topicReferenceId = source.Attributes.GetInteger($"{configuration.AttributeKey}Id", 0);
606+
topicReferenceId = source.Attributes.GetInteger($"{configuration.GetCompositeAttributeKey(attributePrefix)}Id", 0);
607607
}
608608
if (topicReferenceId > 0) {
609609
topicReference = _topicRepository.Load(topicReferenceId, source);
@@ -643,21 +643,21 @@ await MapAsync(
643643
\-----------------------------------------------------------------------------------------------------------------------*/
644644
var typeAccessor = GetTopicAccessor(source.GetType());
645645

646-
var attributeValue = typeAccessor.GetMethodValue(source, $"Get{configuration.AttributeKey}")?.ToString();
646+
var attributeValue = typeAccessor.GetMethodValue(source, $"Get{configuration.GetCompositeAttributeKey(attributePrefix)}")?.ToString();
647647

648648
/*------------------------------------------------------------------------------------------------------------------------
649649
| Attempt to retrieve value from topic.{Property}
650650
\-----------------------------------------------------------------------------------------------------------------------*/
651651
if (attributeValue is null) {
652-
attributeValue = typeAccessor.GetPropertyValue(source, configuration.AttributeKey)?.ToString();
652+
attributeValue = typeAccessor.GetPropertyValue(source, configuration.GetCompositeAttributeKey(attributePrefix))?.ToString();
653653
}
654654

655655
/*------------------------------------------------------------------------------------------------------------------------
656656
| Otherwise, attempt to retrieve value from topic.Attributes.GetValue({Property})
657657
\-----------------------------------------------------------------------------------------------------------------------*/
658658
if (attributeValue is null) {
659659
attributeValue = source.Attributes.GetValue(
660-
configuration.AttributeKey,
660+
configuration.GetCompositeAttributeKey(attributePrefix),
661661
configuration.DefaultValue?.ToString(),
662662
configuration.InheritValue
663663
);
@@ -848,7 +848,7 @@ private IList<Topic> GetSourceCollection(
848848
//For example, the ContentTypeDescriptor's AttributeDescriptors collection, which provides a rollup of
849849
//AttributeDescriptors from the current ContentTypeDescriptor, as well as all of its ascendents.
850850
if (listSource.Count == 0) {
851-
var sourceProperty = TypeAccessorCache.GetTypeAccessor(source.GetType()).GetMember(configuration.AttributeKey);
851+
var sourceProperty = TypeAccessorCache.GetTypeAccessor(source.GetType()).GetMember(configuration.GetCompositeAttributeKey(attributePrefix));
852852
if (
853853
sourceProperty?.GetValue(source) is IList sourcePropertyValue &&
854854
sourcePropertyValue.Count > 0 &&
@@ -1132,7 +1132,7 @@ private static bool TryGetCompatibleProperty(Topic source, Type targetType, Item
11321132
/*------------------------------------------------------------------------------------------------------------------------
11331133
| Attempt to retrieve value from topic.{Property}
11341134
\-----------------------------------------------------------------------------------------------------------------------*/
1135-
var sourcePropertyAccessor = GetTopicAccessor(source.GetType()).GetMember(configuration.AttributeKey);
1135+
var sourcePropertyAccessor = GetTopicAccessor(source.GetType()).GetMember(configuration.GetCompositeAttributeKey(attributePrefix));
11361136

11371137
/*------------------------------------------------------------------------------------------------------------------------
11381138
| Escape clause if preconditions are not met

0 commit comments

Comments
 (0)