Skip to content

Commit 10377d9

Browse files
committed
Replaced conditions with pattern matching
1 parent d8d41b7 commit 10377d9

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

OnTopic.AspNetCore.Mvc.Host/SampleActivator.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,13 @@ public object Create(ControllerContext context) {
9797
/*------------------------------------------------------------------------------------------------------------------------
9898
| Configure and return appropriate controller
9999
\-----------------------------------------------------------------------------------------------------------------------*/
100-
if (type == typeof(TopicController)) {
101-
return new TopicController(_topicRepository, _topicMappingService);
102-
}
103-
if (type == typeof(SitemapController)) {
104-
return new SitemapController(_topicRepository);
105-
}
106-
else {
107-
throw new Exception($"Unknown controller {type.Name}");
108-
}
100+
return type.Name switch {
101+
nameof(TopicController) =>
102+
new TopicController(_topicRepository, _topicMappingService),
103+
nameof(SitemapController) =>
104+
new SitemapController(_topicRepository),
105+
_ => throw new Exception($"Unknown controller {type.Name}")
106+
};
109107

110108
}
111109

@@ -123,15 +121,13 @@ public object Create(ViewComponentContext context) {
123121
/*------------------------------------------------------------------------------------------------------------------------
124122
| Configure and return appropriate view component
125123
\-----------------------------------------------------------------------------------------------------------------------*/
126-
if (type == typeof(MenuViewComponent)) {
127-
return new MenuViewComponent(_topicRepository, _hierarchicalMappingService);
128-
}
129-
else if (type == typeof(PageLevelNavigationViewComponent)) {
130-
return new PageLevelNavigationViewComponent(_topicRepository, _hierarchicalMappingService);
131-
}
132-
else {
133-
throw new Exception($"Unknown view component {type.Name}");
134-
}
124+
return type.Name switch {
125+
nameof(MenuViewComponent) =>
126+
new MenuViewComponent(_topicRepository, _hierarchicalMappingService),
127+
nameof(PageLevelNavigationViewComponent) =>
128+
new PageLevelNavigationViewComponent(_topicRepository, _hierarchicalMappingService),
129+
_ => throw new Exception($"Unknown view component {type.Name}")
130+
};
135131

136132
}
137133

0 commit comments

Comments
 (0)