Review a generated command router

from Pattern matching
C# 14 / .NET 10 advanced 8 min 4 issues to find

Review this generated switch against the command-routing contract.

Reject null commands, require a positive refund amount, authorize admin commands only with the trusted method parameter, require a nonempty user ID for ordinary commands, and reject unknown input explicitly.

csharp
using System;

public sealed record Command(
    string Name,
    string? UserId,
    decimal Amount,
    bool IsAdmin);

public static class CommandRouter
{
    public static string Route(Command command, bool isAuthorizedAdmin)
    {
        ArgumentNullException.ThrowIfNull(command);

        return command switch
        {
            { Name: "refund", Amount: >= 0m } => "refund",
            { Name: "admin", IsAdmin: true } => "admin",
            { UserId: not null } => "user",
            _ => "ignored"
        };
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error