Skip to content

Commit a31864a

Browse files
committed
Implemented GetValidatedMappingType() for topic references
The `GetTopicReferenceAsync()` method takes a topic reference from the source topic and maps it to a property on the target model. If the mapped topic type isn't compatible with the target property type, then it is excluded, as a way of filtering out incompatible types. By incorporating the new `GetValidatedMappingType()` method (88c7fba), we pre-validate the type compatibility, instead of requiring that the model first be mapped—a potentially expensive operation. This satisfies the topic reference requirement called for in #83.
1 parent 7caa832 commit a31864a

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,23 +1085,17 @@ MappedTopicCache cache
10851085
/*------------------------------------------------------------------------------------------------------------------------
10861086
| Map referenced topic
10871087
\-----------------------------------------------------------------------------------------------------------------------*/
1088-
var topicDto = (object?)null;
1089-
try {
1090-
if (configuration.MapAs != null) {
1091-
topicDto = await MapAsync(source, configuration.MapAs, configuration.IncludeAssociations, cache).ConfigureAwait(false);
1092-
}
1093-
else {
1094-
topicDto = await MapAsync(source, configuration.IncludeAssociations, cache).ConfigureAwait(false);
1095-
}
1096-
}
1097-
catch (InvalidTypeException) {
1098-
//Disregard errors caused by unmapped view models; those are functionally equivalent to IsAssignableFrom() mismatches
1088+
var topicDto = (object?)null;
1089+
var mappingType = GetValidatedMappingType(configuration.MapAs, targetType)?? GetValidatedMappingType(source, targetType);
1090+
1091+
if (mappingType is not null) {
1092+
topicDto = await MapAsync(source, mappingType, configuration.IncludeAssociations, cache).ConfigureAwait(false);
10991093
}
11001094

11011095
/*------------------------------------------------------------------------------------------------------------------------
11021096
| Validate results
11031097
\-----------------------------------------------------------------------------------------------------------------------*/
1104-
if (topicDto is null || !targetType.IsAssignableFrom(topicDto.GetType())) {
1098+
if (topicDto is null) {
11051099
return null;
11061100
}
11071101

0 commit comments

Comments
 (0)