Skip to content

Commit 751c259

Browse files
committed
Retrofitted ReverseTopicMappingService to use new PropertyConfiguration
In #8bb6ab4, `PropertyConfiguration` was updated to be made aware of the contextual `AttributePrefix`, thus allowing the `attributePrefix` to be relayed (and composited) via the `PropertyConfiguration` instead of by passing it to each helper method, and then prepending it to every call to `configuration.AttributeKey`. This is much more elegant, and more intuitive from an implementation perspective. This doesn't change the functionality, just refactors the implementation so that the `attributePrefix` is more deeply integrated into the infrastructure—which, conveniently, makes it so that most downstream elements no longer need to be aware of the `attributePrefix`. As an example of this, we were able to remove `attributePrefix` entirely from the `BindingModelValidator` since it now infers it via the `PropertyConfiguration.AttributeKey`.
1 parent 8bb6ab4 commit 751c259

2 files changed

Lines changed: 24 additions & 37 deletions

File tree

OnTopic/Mapping/BindingModelValidator.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,10 @@ static internal class BindingModelValidator {
8787
/// <param name="contentTypeDescriptor">
8888
/// The <see cref="ContentTypeDescriptor"/> object against which to validate the model.
8989
/// </param>
90-
/// <param name="attributePrefix">The optional prefix to apply to the attributes.</param>
9190
static internal void ValidateModel(
9291
[AllowNull]Type sourceType,
9392
[AllowNull]MemberInfoCollection<PropertyInfo> properties,
94-
[AllowNull]ContentTypeDescriptor contentTypeDescriptor,
95-
string? attributePrefix = null
93+
[AllowNull]ContentTypeDescriptor contentTypeDescriptor
9694
) {
9795

9896
/*------------------------------------------------------------------------------------------------------------------------
@@ -113,7 +111,7 @@ static internal void ValidateModel(
113111
| Validate
114112
\-----------------------------------------------------------------------------------------------------------------------*/
115113
foreach (var property in properties) {
116-
ValidateProperty(sourceType, property, contentTypeDescriptor, attributePrefix);
114+
ValidateProperty(sourceType, property, contentTypeDescriptor);
117115
}
118116

119117
/*------------------------------------------------------------------------------------------------------------------------
@@ -141,12 +139,10 @@ static internal void ValidateModel(
141139
/// <param name="contentTypeDescriptor">
142140
/// The <see cref="ContentTypeDescriptor"/> object against which to validate the model.
143141
/// </param>
144-
/// <param name="attributePrefix">The optional prefix to apply to the attributes.</param>
145142
static internal void ValidateProperty(
146143
[AllowNull]Type sourceType,
147144
[AllowNull]PropertyInfo property,
148-
[AllowNull]ContentTypeDescriptor contentTypeDescriptor,
149-
string? attributePrefix
145+
[AllowNull]ContentTypeDescriptor contentTypeDescriptor
150146
) {
151147

152148
/*------------------------------------------------------------------------------------------------------------------------
@@ -161,7 +157,7 @@ static internal void ValidateProperty(
161157
\-----------------------------------------------------------------------------------------------------------------------*/
162158
var propertyType = property.PropertyType;
163159
var configuration = new PropertyConfiguration(property);
164-
var compositeAttributeKey = attributePrefix + configuration.AttributeKey;
160+
var compositeAttributeKey = configuration.AttributeKey;
165161
var attributeDescriptor = contentTypeDescriptor.AttributeDescriptors.GetTopic(compositeAttributeKey);
166162
var childRelationships = new[] { RelationshipType.Children, RelationshipType.NestedTopics };
167163
var relationships = new[] { RelationshipType.Relationship, RelationshipType.IncomingRelationship };
@@ -182,8 +178,7 @@ static internal void ValidateProperty(
182178
ValidateModel(
183179
propertyType,
184180
childProperties,
185-
contentTypeDescriptor,
186-
attributePrefix + configuration.AttributePrefix
181+
contentTypeDescriptor
187182
);
188183
return;
189184
}

OnTopic/Mapping/Reverse/ReverseTopicMappingService.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public ReverseTopicMappingService(ITopicRepository topicRepository) {
213213
var properties = _typeCache.GetMembers<PropertyInfo>(source.GetType());
214214
var contentTypeDescriptor = _contentTypeDescriptors.GetTopic(target.ContentType);
215215

216-
BindingModelValidator.ValidateModel(source.GetType(), properties, contentTypeDescriptor, attributePrefix);
216+
BindingModelValidator.ValidateModel(source.GetType(), properties, contentTypeDescriptor);
217217

218218
/*------------------------------------------------------------------------------------------------------------------------
219219
| Loop through properties, mapping each one
@@ -262,9 +262,9 @@ protected async Task SetPropertyAsync(
262262
/*------------------------------------------------------------------------------------------------------------------------
263263
| Establish per-property variables
264264
\-----------------------------------------------------------------------------------------------------------------------*/
265-
var configuration = new PropertyConfiguration(property);
265+
var configuration = new PropertyConfiguration(property, attributePrefix);
266266
var contentTypeDescriptor = _contentTypeDescriptors.GetTopic(target.ContentType);
267-
var compositeAttributeKey = attributePrefix + configuration.AttributeKey;
267+
var compositeAttributeKey = configuration.AttributeKey;
268268

269269
Contract.Assume(contentTypeDescriptor, nameof(contentTypeDescriptor));
270270

@@ -282,7 +282,7 @@ protected async Task SetPropertyAsync(
282282
await MapAsync(
283283
property.GetValue(source),
284284
target,
285-
attributePrefix + configuration.AttributePrefix
285+
configuration.AttributePrefix
286286
).ConfigureAwait(false);
287287
return;
288288
}
@@ -308,16 +308,16 @@ await MapAsync(
308308
\-----------------------------------------------------------------------------------------------------------------------*/
309309
switch (attributeType.ModelType) {
310310
case ModelType.ScalarValue:
311-
SetScalarValue(source, target, configuration, attributePrefix);
311+
SetScalarValue(source, target, configuration);
312312
return;
313313
case ModelType.Relationship:
314-
SetRelationships(source, target, configuration, attributePrefix);
314+
SetRelationships(source, target, configuration);
315315
return;
316316
case ModelType.NestedTopic:
317-
await SetNestedTopicsAsync(source, target, configuration, attributePrefix).ConfigureAwait(false);
317+
await SetNestedTopicsAsync(source, target, configuration).ConfigureAwait(false);
318318
return;
319319
case ModelType.Reference:
320-
SetReference(source, target, configuration, attributePrefix);
320+
SetReference(source, target, configuration);
321321
return;
322322
}
323323

@@ -332,7 +332,7 @@ await MapAsync(
332332
/// <remarks>
333333
/// Assuming the <paramref name="configuration"/>'s <see cref="PropertyConfiguration.Property"/> is of the type <see
334334
/// cref="String"/>, <see cref="Boolean"/>, <see cref="Int32"/>, or <see cref="DateTime"/>, the <see
335-
/// cref="SetScalarValue(Object, Topic, PropertyConfiguration, String)"/> method will attempt to set the property on the
335+
/// cref="SetScalarValue(Object, Topic, PropertyConfiguration)"/> method will attempt to set the property on the
336336
/// <paramref name="target"/>. If the value is not set on the <paramref name="source"/> then the <see
337337
/// cref="DefaultValueAttribute"/> will be evaluated as a fallback. If the property is not of a settable type then the
338338
/// property is not set. If the value is empty, then it will be treated as <c>null</c> in the <paramref name="target"/>'s
@@ -343,13 +343,11 @@ await MapAsync(
343343
/// </param>
344344
/// <param name="target">The <see cref="Topic"/> entity to map the data to.</param>
345345
/// <param name="configuration">The <see cref="PropertyConfiguration"/> with details about the property's attributes.</param>
346-
/// <param name="attributePrefix">The prefix to apply to the attributes.</param>
347346
/// <autogeneratedoc />
348347
protected static void SetScalarValue(
349348
object source,
350349
Topic target,
351-
PropertyConfiguration configuration,
352-
string? attributePrefix = null
350+
PropertyConfiguration configuration
353351
)
354352
{
355353

@@ -386,7 +384,7 @@ protected static void SetScalarValue(
386384
/*------------------------------------------------------------------------------------------------------------------------
387385
| Set the value (to null, if appropriate)
388386
\-----------------------------------------------------------------------------------------------------------------------*/
389-
target.Attributes.SetValue(attributePrefix + configuration.AttributeKey, attributeValue);
387+
target.Attributes.SetValue(configuration.AttributeKey, attributeValue);
390388

391389
}
392390

@@ -404,12 +402,10 @@ protected static void SetScalarValue(
404402
/// <param name="configuration">
405403
/// The <see cref="PropertyConfiguration"/> with details about the property's attributes.
406404
/// </param>
407-
/// <param name="attributePrefix">The prefix to apply to the attributes.</param>
408405
protected void SetRelationships(
409406
object source,
410407
Topic target,
411-
PropertyConfiguration configuration,
412-
string? attributePrefix = null
408+
PropertyConfiguration configuration
413409
)
414410
{
415411

@@ -432,7 +428,7 @@ protected void SetRelationships(
432428
/*------------------------------------------------------------------------------------------------------------------------
433429
| Clear existing relationships
434430
\-----------------------------------------------------------------------------------------------------------------------*/
435-
target.Relationships.ClearTopics(attributePrefix + configuration.AttributeKey);
431+
target.Relationships.ClearTopics(configuration.AttributeKey);
436432

437433
/*------------------------------------------------------------------------------------------------------------------------
438434
| Set relationships for each
@@ -446,7 +442,7 @@ protected void SetRelationships(
446442
$"be located in the repository."
447443
);
448444
}
449-
target.Relationships.SetTopic(attributePrefix + configuration.AttributeKey, targetTopic);
445+
target.Relationships.SetTopic(configuration.AttributeKey, targetTopic);
450446
}
451447

452448
}
@@ -465,12 +461,10 @@ protected void SetRelationships(
465461
/// <param name="configuration">
466462
/// The <see cref="PropertyConfiguration"/> with details about the property's attributes.
467463
/// </param>
468-
/// <param name="attributePrefix">The prefix to apply to the attributes.</param>
469464
protected async Task SetNestedTopicsAsync(
470465
object source,
471466
Topic target,
472-
PropertyConfiguration configuration,
473-
string? attributePrefix = null
467+
PropertyConfiguration configuration
474468
) {
475469

476470
/*------------------------------------------------------------------------------------------------------------------------
@@ -488,9 +482,9 @@ protected async Task SetNestedTopicsAsync(
488482
/*------------------------------------------------------------------------------------------------------------------------
489483
| Establish target collection to store mapped topics
490484
\-----------------------------------------------------------------------------------------------------------------------*/
491-
var container = target.Children.GetTopic(attributePrefix + configuration.AttributeKey);
485+
var container = target.Children.GetTopic(configuration.AttributeKey);
492486
if (container == null) {
493-
container = TopicFactory.Create(attributePrefix + configuration.AttributeKey, "List", target);
487+
container = TopicFactory.Create(configuration.AttributeKey, "List", target);
494488
container.IsHidden = true;
495489
}
496490

@@ -515,12 +509,10 @@ protected async Task SetNestedTopicsAsync(
515509
/// <param name="configuration">
516510
/// The <see cref="PropertyConfiguration"/> with details about the property's attributes.
517511
/// </param>
518-
/// <param name="attributePrefix">The prefix to apply to the attributes.</param>
519512
protected void SetReference(
520513
object source,
521514
Topic target,
522-
PropertyConfiguration configuration,
523-
string? attributePrefix = null
515+
PropertyConfiguration configuration
524516
) {
525517

526518
/*------------------------------------------------------------------------------------------------------------------------
@@ -560,7 +552,7 @@ protected void SetReference(
560552
/*------------------------------------------------------------------------------------------------------------------------
561553
| Set target attribute
562554
\-----------------------------------------------------------------------------------------------------------------------*/
563-
target.Attributes.SetInteger(attributePrefix + configuration.AttributeKey, topicReference.Id);
555+
target.Attributes.SetInteger(configuration.AttributeKey, topicReference.Id);
564556

565557
}
566558

0 commit comments

Comments
 (0)