Skip to content

Commit 0539c8d

Browse files
committed
Established cached version of TypeAccessor for Topic
When evaluating members of the source `Topic` class in `TopicMappingService`, we are almost always operating off of an actual `Topic` class, since subclasses of `Topic` are rare, and mostly used for configuration data. Given that, it doesn't make sense to do a lookup of the `TypeAccessor` for the `Topic` class via the `TypeAccessorCache` every time we need to do this (e.g., as part of `GetValue()` and `TryGetCompatibleValue()`. This lookup was probably fast, but there's no need to repeat it when we have a logical default.
1 parent d0fb8a4 commit 0539c8d

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class TopicMappingService : ITopicMappingService {
3030
\-------------------------------------------------------------------------------------------------------------------------*/
3131
readonly ITopicRepository _topicRepository;
3232
readonly ITypeLookupService _typeLookupService;
33+
static readonly TypeAccessor _topicTypeAccessor = TypeAccessorCache.GetTypeAccessor<Topic>();
3334

3435
/*==========================================================================================================================
3536
| CONSTRUCTOR
@@ -600,7 +601,8 @@ await MapAsync(
600601
/*------------------------------------------------------------------------------------------------------------------------
601602
| Attempt to retrieve value from topic.Get{Property}()
602603
\-----------------------------------------------------------------------------------------------------------------------*/
603-
var typeAccessor = TypeAccessorCache.GetTypeAccessor(source.GetType());
604+
var typeAccessor = GetTopicAccessor(source.GetType());
605+
604606
var attributeValue = typeAccessor.GetMethodValue(source, $"Get{configuration.AttributeKey}")?.ToString();
605607

606608
/*------------------------------------------------------------------------------------------------------------------------
@@ -1071,7 +1073,7 @@ private static bool TryGetCompatibleProperty(Topic source, Type targetType, Item
10711073
/*------------------------------------------------------------------------------------------------------------------------
10721074
| Attempt to retrieve value from topic.{Property}
10731075
\-----------------------------------------------------------------------------------------------------------------------*/
1074-
var sourcePropertyAccessor = TypeAccessorCache.GetTypeAccessor(source.GetType()).GetMember(configuration.AttributeKey);
1076+
var sourcePropertyAccessor = GetTopicAccessor(source.GetType()).GetMember(configuration.AttributeKey);
10751077

10761078
/*------------------------------------------------------------------------------------------------------------------------
10771079
| Escape clause if preconditions are not met
@@ -1090,5 +1092,16 @@ private static bool TryGetCompatibleProperty(Topic source, Type targetType, Item
10901092

10911093
}
10921094

1095+
/*==========================================================================================================================
1096+
| PRIVATE: GET TOPIC ACCESSOR
1097+
\-------------------------------------------------------------------------------------------------------------------------*/
1098+
/// <summary>
1099+
/// Given a specific <see cref="Type"/> for a <see cref="Topic"/>, returns the appropriate <see cref="TypeAccessor"/> from
1100+
/// the <see cref="TypeAccessorCache"/>.
1101+
/// </summary>
1102+
/// <param name="topicType">The <see cref="Type"/> of the source <see cref="Topic"/>.</param>
1103+
private static TypeAccessor GetTopicAccessor(Type topicType) =>
1104+
topicType == typeof(Topic) ? _topicTypeAccessor : TypeAccessorCache.GetTypeAccessor(topicType);
1105+
10931106
} //Class
10941107
} //Namespace

0 commit comments

Comments
 (0)