Skip to content

Commit 48f7c5c

Browse files
committed
Preferred generic MapAsync<>() in favor of casting to a view model
In terms of the test case, this generally yields the same outcome. It's a bit faster, however, as it doesn't need to rely on the `ITypeLookupService` to determine the appropriate topic to create, nor reflection to create that topic. I also find the syntax just a tiny bit more readible—though that's a pretty nit-picky difference here. The one obvious exception is `MapDynamic()` since it is explicitly intended to test the ability to dynamically determine the correct view model based on the view model name.
1 parent 4eff555 commit 48f7c5c

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public async Task MapParents() {
118118

119119
grandParent.Attributes.SetValue("IsRoot", "1");
120120

121-
var viewModel = (AscendentTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
121+
var viewModel = await _mappingService.MapAsync<AscendentTopicViewModel>(topic).ConfigureAwait(false);
122122
var parentViewModel = viewModel?.Parent;
123123
var grandParentViewModel = parentViewModel?.Parent as AscendentSpecializedTopicViewModel;
124124

@@ -150,7 +150,7 @@ public async Task InheritValues() {
150150
grandParent.Attributes.SetValue("Property", "ValueA");
151151
grandParent.Attributes.SetValue("InheritedProperty", "ValueB");
152152

153-
var viewModel = (InheritedPropertyTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
153+
var viewModel = await _mappingService.MapAsync<InheritedPropertyTopicViewModel>(topic).ConfigureAwait(false);
154154

155155
Assert.IsNull(viewModel.Property);
156156
Assert.AreEqual<string>("ValueB", viewModel.InheritedProperty);
@@ -241,7 +241,7 @@ public async Task AlternateAttributeKey() {
241241
topic.Attributes.SetValue("Property", "ValueA");
242242
topic.Attributes.SetValue("PropertyAlias", "ValueB");
243243

244-
var viewModel = (PropertyAliasTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
244+
var viewModel = await _mappingService.MapAsync<PropertyAliasTopicViewModel>(topic).ConfigureAwait(false);
245245

246246
Assert.AreEqual<string>("ValueA", viewModel.PropertyAlias);
247247

@@ -265,7 +265,7 @@ public async Task MapRelationships() {
265265
topic.Relationships.SetTopic("Cousins", relatedTopic2);
266266
topic.Relationships.SetTopic("Siblings", relatedTopic3);
267267

268-
var target = (RelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
268+
var target = await _mappingService.MapAsync<RelationTopicViewModel>(topic).ConfigureAwait(false);
269269

270270
Assert.AreEqual<int>(2, target.Cousins.Count);
271271
Assert.IsNotNull(GetChildTopic(target.Cousins, "Cousin1"));
@@ -306,7 +306,7 @@ public async Task AlternateRelationship() {
306306
ambiguousRelation.Relationships.SetTopic("RelationshipAlias", topic);
307307
incomingRelation.Relationships.SetTopic("AmbiguousRelationship", topic);
308308

309-
var target = (AmbiguousRelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
309+
var target = await _mappingService.MapAsync<AmbiguousRelationTopicViewModel>(topic).ConfigureAwait(false);
310310

311311
Assert.AreEqual<int>(1, target.RelationshipAlias.Count);
312312
Assert.IsNotNull(GetChildTopic(target.RelationshipAlias, "IncomingRelation"));
@@ -324,7 +324,7 @@ public async Task AlternateRelationship() {
324324
public async Task CustomCollection() {
325325

326326
var topic = _topicRepository.Load("Root:Configuration:ContentTypes:Page");
327-
var target = (ContentTypeDescriptorTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
327+
var target = await _mappingService.MapAsync<ContentTypeDescriptorTopicViewModel>(topic).ConfigureAwait(false);
328328

329329
Assert.AreEqual<int>(8, target.AttributeDescriptors.Count);
330330
Assert.AreEqual<int>(2, target.PermittedContentTypes.Count);
@@ -351,7 +351,7 @@ public async Task MapNestedTopics() {
351351

352352
topicList.IsHidden = true;
353353

354-
var target = (NestedTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
354+
var target = await _mappingService.MapAsync<NestedTopicViewModel>(topic).ConfigureAwait(false);
355355

356356
Assert.AreEqual<int>(2, target.Categories.Count);
357357

@@ -381,7 +381,7 @@ public async Task MapChildren() {
381381

382382
childTopic4.Attributes.SetBoolean("IsLeaf", true);
383383

384-
var target = (DescendentTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
384+
var target = await _mappingService.MapAsync<DescendentTopicViewModel>(topic).ConfigureAwait(false);
385385

386386
Assert.AreEqual<int>(4, target.Children.Count);
387387
Assert.IsNotNull(GetChildTopic(target.Children, "ChildTopic1"));
@@ -455,7 +455,7 @@ public async Task RecursiveRelationships() {
455455
//Set ancillary relationships
456456
cousinTopic3.Relationships.SetTopic("Cousins", secondCousin);
457457

458-
var target = (RelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
458+
var target = await _mappingService.MapAsync<RelationTopicViewModel>(topic).ConfigureAwait(false);
459459

460460
var cousinTarget = GetChildTopic(target.Cousins, "CousinTopic3") as RelationWithChildrenTopicViewModel;
461461
var distantCousinTarget = GetChildTopic(cousinTarget.Children, "ChildTopic3") as RelationWithChildrenTopicViewModel;
@@ -489,7 +489,7 @@ public async Task MapSlideshow() {
489489
var childTopic3 = TopicFactory.Create("ChildTopic3", "Slide", slides);
490490
var childTopic4 = TopicFactory.Create("ChildTopic4", "ContentItem", slides);
491491

492-
var target = (SlideshowTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
492+
var target = await _mappingService.MapAsync<SlideshowTopicViewModel>(topic).ConfigureAwait(false);
493493

494494
Assert.AreEqual<int>(4, target.ContentItems.Count);
495495
Assert.IsNotNull(GetChildTopic(target.ContentItems, "ChildTopic1"));
@@ -518,7 +518,7 @@ public async Task MapTopics() {
518518
topic.Relationships.SetTopic("RelatedTopics", relatedTopic2);
519519
topic.Relationships.SetTopic("RelatedTopics", relatedTopic3);
520520

521-
var target = (RelatedEntityTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
521+
var target = await _mappingService.MapAsync<RelatedEntityTopicViewModel>(topic).ConfigureAwait(false);
522522
var relatedTopic3copy = (getRelatedTopic(target, "RelatedTopic3"));
523523

524524
Assert.AreEqual<int>(3, target.RelatedTopics.Count);
@@ -566,7 +566,7 @@ public async Task MapCircularReference() {
566566
var topic = TopicFactory.Create("Test", "Circular", 1);
567567
var childTopic = TopicFactory.Create("ChildTopic", "Circular", 2, topic);
568568

569-
var mappedTopic = (CircularTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
569+
var mappedTopic = await _mappingService.MapAsync<CircularTopicViewModel>(topic).ConfigureAwait(false);
570570

571571
Assert.AreEqual<CircularTopicViewModel>(mappedTopic, mappedTopic.Children.First().Parent);
572572

@@ -588,7 +588,7 @@ public async Task FilterByContentType() {
588588
var childTopic3 = TopicFactory.Create("ChildTopic3", "DescendentSpecialized", topic);
589589
var childTopic4 = TopicFactory.Create("ChildTopic4", "DescendentSpecialized", childTopic3);
590590

591-
var target = (DescendentTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
591+
var target = await _mappingService.MapAsync<DescendentTopicViewModel>(topic).ConfigureAwait(false);
592592

593593
var specialized = target.Children.GetByContentType("DescendentSpecialized");
594594

@@ -613,7 +613,7 @@ public async Task MapGetterMethods() {
613613
var childTopic = TopicFactory.Create("Child", "Page", topic);
614614
var grandChildTopic = TopicFactory.Create("GrandChild", "Index", childTopic);
615615

616-
var target = (IndexTopicViewModel?)await _mappingService.MapAsync(grandChildTopic).ConfigureAwait(false);
616+
var target = await _mappingService.MapAsync<IndexTopicViewModel>(grandChildTopic).ConfigureAwait(false);
617617

618618
Assert.AreEqual<string>("Topic:Child:GrandChild", target.UniqueKey);
619619

@@ -633,7 +633,7 @@ public async Task MapCompatibleProperties() {
633633

634634
topic.VersionHistory.Add(new DateTime(1976, 10, 15, 9, 30, 00));
635635

636-
var target = (CompatiblePropertyTopicViewModel?)await _mappingService.MapAsync<CompatiblePropertyTopicViewModel>(topic).ConfigureAwait(false);
636+
var target = await _mappingService.MapAsync<CompatiblePropertyTopicViewModel>(topic).ConfigureAwait(false);
637637

638638
Assert.AreEqual<ModelType>(topic.ModelType, target.ModelType);
639639
Assert.AreEqual<int>(1, target.VersionHistory.Count);
@@ -653,7 +653,7 @@ public async Task MapRequiredProperty() {
653653

654654
topic.Attributes.SetValue("RequiredAttribute", "Required");
655655

656-
var target = (RequiredTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
656+
var target = await _mappingService.MapAsync<RequiredTopicViewModel>(topic).ConfigureAwait(false);
657657

658658
Assert.AreEqual<string>("Required", target.RequiredAttribute);
659659

@@ -671,7 +671,7 @@ public async Task MapRequiredPropertyException() {
671671

672672
var topic = TopicFactory.Create("Topic", "Required");
673673

674-
var target = (RequiredTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
674+
var target = await _mappingService.MapAsync<RequiredTopicViewModel>(topic).ConfigureAwait(false);
675675

676676
}
677677

@@ -687,7 +687,7 @@ public async Task MapRequiredObjectPropertyException() {
687687

688688
var topic = TopicFactory.Create("Topic", "RequiredObject");
689689

690-
var target = (RequiredTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
690+
var target = await _mappingService.MapAsync<RequiredTopicViewModel>(topic).ConfigureAwait(false);
691691

692692
}
693693

@@ -702,7 +702,7 @@ public async Task MapDefaultValueProperties() {
702702

703703
var topic = TopicFactory.Create("Topic", "DefaultValue");
704704

705-
var target = (DefaultValueTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
705+
var target = await _mappingService.MapAsync<DefaultValueTopicViewModel>(topic).ConfigureAwait(false);
706706

707707
Assert.AreEqual<string>("Default", target.DefaultString);
708708
Assert.AreEqual<int>(10, target.DefaultInt);
@@ -724,7 +724,7 @@ public async Task MapMinimumValueProperties() {
724724

725725
topic.Attributes.SetValue("MinimumLength", "Hello World");
726726

727-
var target = (MinimumLengthPropertyTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
727+
var target = await _mappingService.MapAsync<MinimumLengthPropertyTopicViewModel>(topic).ConfigureAwait(false);
728728

729729
}
730730

@@ -750,7 +750,7 @@ public async Task FilterByAttribute() {
750750
childTopic3.Attributes.SetValue("SomeAttribute", "ValueA");
751751
childTopic4.Attributes.SetValue("SomeAttribute", "ValueB");
752752

753-
var target = (FilteredTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
753+
var target = await _mappingService.MapAsync<FilteredTopicViewModel>(topic).ConfigureAwait(false);
754754

755755
Assert.AreEqual<int>(2, target.Children.Count);
756756

@@ -775,7 +775,7 @@ public async Task Flatten() {
775775
}
776776
}
777777

778-
var target = (FlattenChildrenTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
778+
var target = await _mappingService.MapAsync<FlattenChildrenTopicViewModel>(topic).ConfigureAwait(false);
779779

780780
Assert.AreEqual<int>(25, target.Children.Count);
781781

0 commit comments

Comments
 (0)