Skip to content

Commit 1fed75b

Browse files
committed
IDE0019: Use pattern matching
Implemented pattern matching as part of validation of `context.Controller` in the filter actions. As part of this, reorganized how the variables are defined in `[ValidateTopic]` for consistency.
1 parent 22a24ab commit 1fed75b

2 files changed

Lines changed: 7 additions & 13 deletions

File tree

OnTopic.AspNetCore.Mvc/_filters/TopicResponseCacheAttribute.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public override void OnActionExecuting(ActionExecutingContext context) {
6262
/*------------------------------------------------------------------------------------------------------------------------
6363
| Validate controller
6464
\-----------------------------------------------------------------------------------------------------------------------*/
65-
var controller = context.Controller as TopicController;
66-
67-
if (controller is null) {
65+
if (context.Controller is not TopicController controller) {
6866
throw new InvalidOperationException(
6967
$"The {nameof(TopicResponseCacheAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
7068
);

OnTopic.AspNetCore.Mvc/_filters/ValidateTopicAttribute.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,19 @@ public override void OnActionExecuting(ActionExecutingContext context) {
6060
Contract.Requires(context, nameof(context));
6161

6262
/*------------------------------------------------------------------------------------------------------------------------
63-
| Establish variables
63+
| Validate controller
6464
\-----------------------------------------------------------------------------------------------------------------------*/
65-
var controller = context.Controller as TopicController;
66-
var currentTopic = controller?.CurrentTopic;
67-
68-
/*------------------------------------------------------------------------------------------------------------------------
69-
| Validate context
70-
\-----------------------------------------------------------------------------------------------------------------------*/
71-
if (controller is null) {
65+
if (context.Controller is not TopicController controller) {
7266
throw new InvalidOperationException(
73-
$"The {nameof(ValidateTopicAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
67+
$"The {nameof(TopicResponseCacheAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
7468
);
7569
}
7670

7771
/*------------------------------------------------------------------------------------------------------------------------
78-
| Handle exceptions
72+
| Validate current topic
7973
\-----------------------------------------------------------------------------------------------------------------------*/
74+
var currentTopic = controller.CurrentTopic;
75+
8076
if (currentTopic is null) {
8177
if (!AllowNull) {
8278
context.Result = controller.NotFound("There is no topic associated with this path.");

0 commit comments

Comments
 (0)