Skip to content

Commit 8bb6ab4

Browse files
committed
Incorporated contextual attributePrefix into PropertyConfiguration
Previously, `PropertyConfiguration` was aware of the `AttributePrefix` defined on the local property via the `[MapToParent]` attribute, but it wasn't aware of any _contextual_ `attributePrefix` parameters that the property might be inheriting (i.e., from a parent property). The consequence of this is that the `AttributeKey` didn't necessarily reflect the _actual_ attribute key, and thus the `attributePrefix` needed to be relayed and prepended anywhere that `AttributeKey` was called. This is silly. The updated model allows the `attributePrefix` to optionally be sent to the `PropertyConfiguration` constructor and, if present, it is automatically prepended to both the `AttributePrefix` (to faciliate deep models) as well as the `AttributeKey` (so that the `AttributeKey` always reflects the actual key of the target attribute). Because the `attributePrefix` parameter defaults to empty, this won't affect any prefix implementations until they start passing the `attributePrefix` parameter.
1 parent ce26dbc commit 8bb6ab4

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

OnTopic/Internal/Mapping/PropertyConfiguration.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public class PropertyConfiguration {
4747
/// instances.
4848
/// </summary>
4949
/// <param name="property">The <see cref="PropertyInfo"/> instance to check for <see cref="Attribute"/> values.</param>
50-
public PropertyConfiguration(PropertyInfo property) {
50+
/// <param name="attributePrefix">The prefix to apply to the attributes.</param>
51+
public PropertyConfiguration(PropertyInfo property, string? attributePrefix = "") {
5152

5253
/*------------------------------------------------------------------------------------------------------------------------
5354
| Validate parameters
@@ -62,7 +63,8 @@ public PropertyConfiguration(PropertyInfo property) {
6263
/*------------------------------------------------------------------------------------------------------------------------
6364
| Set default values
6465
\-----------------------------------------------------------------------------------------------------------------------*/
65-
AttributeKey = property.Name;
66+
AttributeKey = attributePrefix + property.Name;
67+
AttributePrefix = attributePrefix;
6668
DefaultValue = null;
6769
InheritValue = false;
6870
RelationshipKey = AttributeKey;
@@ -78,9 +80,9 @@ public PropertyConfiguration(PropertyInfo property) {
7880
\-----------------------------------------------------------------------------------------------------------------------*/
7981
GetAttributeValue<DefaultValueAttribute>(property, a => DefaultValue = a.Value);
8082
GetAttributeValue<InheritAttribute>(property, a => InheritValue = true);
81-
GetAttributeValue<AttributeKeyAttribute>(property, a => AttributeKey = a.Value);
83+
GetAttributeValue<AttributeKeyAttribute>(property, a => AttributeKey = attributePrefix + a.Value);
8284
GetAttributeValue<MapToParentAttribute>(property, a => MapToParent = true);
83-
GetAttributeValue<MapToParentAttribute>(property, a => AttributePrefix = a.AttributePrefix?? property.Name);
85+
GetAttributeValue<MapToParentAttribute>(property, a => AttributePrefix += (a.AttributePrefix?? property.Name));
8486
GetAttributeValue<FollowAttribute>(property, a => CrawlRelationships = a.Relationships);
8587
GetAttributeValue<FlattenAttribute>(property, a => FlattenChildren = true);
8688
GetAttributeValue<MetadataAttribute>(property, a => MetadataKey = a.Key);
@@ -177,13 +179,25 @@ public PropertyConfiguration(PropertyInfo property) {
177179
/// (such as <see cref="IList{T}"/>). The <see cref="MapToParentAttribute"/> allows this behavior to be overwritten.
178180
/// When this occurs, the properties of the referenced object are treated as attributes on the topic. To distinguish
179181
/// them from properties on the parent topic, they are (by default) prefixed with the name of the property that the
180-
/// complex object is assigned to. Optionally, however, this can be overridden, or set to <see cref="String.Empty"/>.
182+
/// complex object is assigned to. Optionally, however, this can be overridden, or even set to <see
183+
/// cref="String.Empty"/>. In the latter case, the properties on the child object will be treated as synonymous with the
184+
/// properties on the parent object. And, in fact, the same property could even be applied to <i>both</i> the child
185+
/// object <i>and</i> the parent object—though this probably doesn't make much sense from a modeling perspective.
186+
/// </para>
187+
/// <para>
188+
/// Be aware that the <see cref="AttributePrefix"/> should <i>only</i> apply to actual attributes on the mapped <see
189+
/// cref="Topic"/> entity; it is <i>not</i> intended to be applied to e.g. collections or relationships.
181190
/// </para>
182191
/// <para>
183192
/// The <see cref="AttributePrefix"/> property corresponds to the <see cref="MapToParentAttribute.AttributePrefix"/>
184193
/// property. It can be assigned by decorating a DTO property with e.g. <c>[MapToParent(AttributePrefix
185194
/// = "AlternateAttributeKey")]</c>.
186195
/// </para>
196+
/// <para>
197+
/// The <see cref="AttributePrefix"/> can be compounded. That is, if a complex object is added to another complex object
198+
/// using <see cref="MapToParentAttribute"/>, then the <see cref="AttributePrefix"/> will reflect the combination of
199+
/// those two prefixes. This allows potentially very deep object models.
200+
/// </para>
187201
/// </remarks>
188202
public string? AttributePrefix { get; set; }
189203

0 commit comments

Comments
 (0)