From 21e3171e11f0c53f5bf91e0e97f92c6472ef037e Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 14:47:41 +0100 Subject: [PATCH] Refactor ToHttpMethod to use a dictionary for mappings Replaced the switch statement in the ToHttpMethod method with a case-insensitive dictionary for mapping HTTP method strings to HttpMethod objects. Introduced a private static dictionary to store predefined HTTP methods, including the addition of the CONNECT method. Improved performance and maintainability by leveraging dictionary lookups for faster and cleaner code. --- src/ReC.Application/Common/HttpExtensions.cs | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/ReC.Application/Common/HttpExtensions.cs b/src/ReC.Application/Common/HttpExtensions.cs index 6f3b4b0..f00ba3f 100644 --- a/src/ReC.Application/Common/HttpExtensions.cs +++ b/src/ReC.Application/Common/HttpExtensions.cs @@ -2,16 +2,20 @@ public static class HttpExtensions { - public static HttpMethod ToHttpMethod(this string method) => method.ToUpper() switch + private static readonly Dictionary _methods = new(StringComparer.OrdinalIgnoreCase) { - "GET" => HttpMethod.Get, - "POST" => HttpMethod.Post, - "PUT" => HttpMethod.Put, - "DELETE" => HttpMethod.Delete, - "PATCH" => HttpMethod.Patch, - "HEAD" => HttpMethod.Head, - "OPTIONS" => HttpMethod.Options, - "TRACE" => HttpMethod.Trace, - _ => throw new ArgumentException($"Invalid HTTP method: {method}", nameof(method)), + ["GET"] = HttpMethod.Get, + ["POST"] = HttpMethod.Post, + ["PUT"] = HttpMethod.Put, + ["DELETE"] = HttpMethod.Delete, + ["PATCH"] = HttpMethod.Patch, + ["HEAD"] = HttpMethod.Head, + ["OPTIONS"] = HttpMethod.Options, + ["TRACE"] = HttpMethod.Trace, + ["CONNECT"] = HttpMethod.Connect }; + + public static HttpMethod ToHttpMethod(this string method) => _methods.TryGetValue(method, out var httpMethod) + ? httpMethod + : new HttpMethod(method); }