Skip to content

Commit 554b953

Browse files
authored
Refactor Routes to use ImmutableArray and move caching to InitializeRoutesFromResource (#182)
- Change _routes from ImmutableDictionary to ImmutableArray<Route> - Move null-coalescing cache assignment from GetRoutes() to InitializeRoutesFromResource() - Simplify JSON deserialization with throw expression
1 parent 0f202be commit 554b953

File tree

3 files changed

+7
-37
lines changed

3 files changed

+7
-37
lines changed

src/Devlead.Testing.MockHttp.sln

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Devlead.Testing.MockHttp.slnx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Solution>
2+
<Project Path="Devlead.Testing.MockHttp.Tests/Devlead.Testing.MockHttp.Tests.csproj" />
3+
<Project Path="Devlead.Testing.MockHttp/Devlead.Testing.MockHttp.csproj" />
4+
</Solution>

src/Devlead.Testing.MockHttp/Routes.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,23 @@ out var response
4949
return GetResponseBuilder;
5050
}
5151

52-
private static ImmutableDictionary<(
53-
HttpMethod Method,
54-
string AbsoluteUri
55-
),
56-
Func<HttpRequestMessage, HttpResponseMessage>
57-
>? _routes;
52+
private static ImmutableArray<Route>? _routes;
5853

5954
private static ImmutableDictionary<(
6055
HttpMethod Method,
6156
string AbsoluteUri
6257
),
6358
Func<HttpRequestMessage, HttpResponseMessage>
6459
> GetRoutes()
65-
=> _routes ??= InitializeRoutesFromResource();
60+
=> InitializeRoutesFromResource();
6661

6762

6863
private static ImmutableDictionary<(HttpMethod Method, string PathAndQuery), Func<HttpRequestMessage, HttpResponseMessage>> InitializeRoutesFromResource()
6964
{
7065
var routesJson = Resources<T>.GetString("Routes.json");
7166
ArgumentException.ThrowIfNullOrEmpty(routesJson);
7267

73-
var routes = System.Text.Json.JsonSerializer.Deserialize<Route[]>(routesJson);
74-
ArgumentNullException.ThrowIfNull(routes);
68+
var routes = _routes ??= ImmutableArray.Create(System.Text.Json.JsonSerializer.Deserialize<Route[]>(routesJson) ?? throw new ArgumentNullException(nameof(routesJson)));
7569

7670
var enableRoute = routes
7771
.Aggregate(

0 commit comments

Comments
 (0)