Skip to content

Commit 19d21ad

Browse files
authored
Refactor "Routing/Builder" methods pre conditions (#41783)
1 parent ce38917 commit 19d21ad

5 files changed

+34
-146
lines changed

src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static RouteGroupBuilder MapGroup(this IEndpointRouteBuilder endpoints, s
5151
/// </returns>
5252
public static RouteGroupBuilder MapGroup(this IEndpointRouteBuilder endpoints, RoutePattern prefix)
5353
{
54-
ArgumentNullException.ThrowIfNull(endpoints, nameof(endpoints));
55-
ArgumentNullException.ThrowIfNull(prefix, nameof(prefix));
54+
ArgumentNullException.ThrowIfNull(endpoints);
55+
ArgumentNullException.ThrowIfNull(prefix);
5656

5757
return new(endpoints, prefix);
5858
}
@@ -158,10 +158,7 @@ public static IEndpointConventionBuilder MapMethods(
158158
IEnumerable<string> httpMethods,
159159
RequestDelegate requestDelegate)
160160
{
161-
if (httpMethods == null)
162-
{
163-
throw new ArgumentNullException(nameof(httpMethods));
164-
}
161+
ArgumentNullException.ThrowIfNull(httpMethods);
165162

166163
var builder = endpoints.Map(RoutePatternFactory.Parse(pattern), requestDelegate);
167164
builder.WithDisplayName($"{pattern} HTTP: {string.Join(", ", httpMethods)}");
@@ -198,20 +195,9 @@ public static IEndpointConventionBuilder Map(
198195
RoutePattern pattern,
199196
RequestDelegate requestDelegate)
200197
{
201-
if (endpoints == null)
202-
{
203-
throw new ArgumentNullException(nameof(endpoints));
204-
}
205-
206-
if (pattern == null)
207-
{
208-
throw new ArgumentNullException(nameof(pattern));
209-
}
210-
211-
if (requestDelegate == null)
212-
{
213-
throw new ArgumentNullException(nameof(requestDelegate));
214-
}
198+
ArgumentNullException.ThrowIfNull(endpoints);
199+
ArgumentNullException.ThrowIfNull(pattern);
200+
ArgumentNullException.ThrowIfNull(requestDelegate);
215201

216202
const int defaultOrder = 0;
217203

@@ -346,10 +332,7 @@ public static RouteHandlerBuilder MapMethods(
346332
IEnumerable<string> httpMethods,
347333
Delegate handler)
348334
{
349-
if (httpMethods is null)
350-
{
351-
throw new ArgumentNullException(nameof(httpMethods));
352-
}
335+
ArgumentNullException.ThrowIfNull(httpMethods);
353336

354337
var disableInferredBody = false;
355338
foreach (var method in httpMethods)
@@ -437,15 +420,8 @@ public static RouteHandlerBuilder Map(
437420
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
438421
public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoints, Delegate handler)
439422
{
440-
if (endpoints == null)
441-
{
442-
throw new ArgumentNullException(nameof(endpoints));
443-
}
444-
445-
if (handler == null)
446-
{
447-
throw new ArgumentNullException(nameof(handler));
448-
}
423+
ArgumentNullException.ThrowIfNull(endpoints);
424+
ArgumentNullException.ThrowIfNull(handler);
449425

450426
return endpoints.MapFallback("{*path:nonfile}", handler);
451427
}
@@ -477,20 +453,9 @@ public static RouteHandlerBuilder MapFallback(
477453
string pattern,
478454
Delegate handler)
479455
{
480-
if (endpoints == null)
481-
{
482-
throw new ArgumentNullException(nameof(endpoints));
483-
}
484-
485-
if (pattern == null)
486-
{
487-
throw new ArgumentNullException(nameof(pattern));
488-
}
489-
490-
if (handler == null)
491-
{
492-
throw new ArgumentNullException(nameof(handler));
493-
}
456+
ArgumentNullException.ThrowIfNull(endpoints);
457+
ArgumentNullException.ThrowIfNull(pattern);
458+
ArgumentNullException.ThrowIfNull(handler);
494459

495460
var conventionBuilder = endpoints.Map(pattern, handler);
496461
conventionBuilder.WithDisplayName("Fallback " + pattern);
@@ -506,20 +471,9 @@ private static RouteHandlerBuilder Map(
506471
bool disableInferBodyFromParameters,
507472
IEnumerable<object>? initialEndpointMetadata = null)
508473
{
509-
if (endpoints is null)
510-
{
511-
throw new ArgumentNullException(nameof(endpoints));
512-
}
513-
514-
if (pattern is null)
515-
{
516-
throw new ArgumentNullException(nameof(pattern));
517-
}
518-
519-
if (handler is null)
520-
{
521-
throw new ArgumentNullException(nameof(handler));
522-
}
474+
ArgumentNullException.ThrowIfNull(endpoints);
475+
ArgumentNullException.ThrowIfNull(pattern);
476+
ArgumentNullException.ThrowIfNull(handler);
523477

524478
const int defaultOrder = 0;
525479

src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public static class EndpointRoutingApplicationBuilderExtensions
3737
/// </remarks>
3838
public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
3939
{
40-
if (builder == null)
41-
{
42-
throw new ArgumentNullException(nameof(builder));
43-
}
40+
ArgumentNullException.ThrowIfNull(builder);
4441

4542
VerifyRoutingServicesAreRegistered(builder);
4643

@@ -85,15 +82,8 @@ public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
8582
/// </remarks>
8683
public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
8784
{
88-
if (builder == null)
89-
{
90-
throw new ArgumentNullException(nameof(builder));
91-
}
92-
93-
if (configure == null)
94-
{
95-
throw new ArgumentNullException(nameof(configure));
96-
}
85+
ArgumentNullException.ThrowIfNull(builder);
86+
ArgumentNullException.ThrowIfNull(configure);
9787

9888
VerifyRoutingServicesAreRegistered(builder);
9989

src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,8 @@ public static class FallbackEndpointRouteBuilderExtensions
3737
/// </remarks>
3838
public static IEndpointConventionBuilder MapFallback(this IEndpointRouteBuilder endpoints, RequestDelegate requestDelegate)
3939
{
40-
if (endpoints == null)
41-
{
42-
throw new ArgumentNullException(nameof(endpoints));
43-
}
44-
45-
if (requestDelegate == null)
46-
{
47-
throw new ArgumentNullException(nameof(requestDelegate));
48-
}
40+
ArgumentNullException.ThrowIfNull(endpoints);
41+
ArgumentNullException.ThrowIfNull(requestDelegate);
4942

5043
return endpoints.MapFallback("{*path:nonfile}", requestDelegate);
5144
}
@@ -76,20 +69,9 @@ public static IEndpointConventionBuilder MapFallback(
7669
string pattern,
7770
RequestDelegate requestDelegate)
7871
{
79-
if (endpoints == null)
80-
{
81-
throw new ArgumentNullException(nameof(endpoints));
82-
}
83-
84-
if (pattern == null)
85-
{
86-
throw new ArgumentNullException(nameof(pattern));
87-
}
88-
89-
if (requestDelegate == null)
90-
{
91-
throw new ArgumentNullException(nameof(requestDelegate));
92-
}
72+
ArgumentNullException.ThrowIfNull(endpoints);
73+
ArgumentNullException.ThrowIfNull(pattern);
74+
ArgumentNullException.ThrowIfNull(requestDelegate);
9375

9476
var conventionBuilder = endpoints.Map(pattern, requestDelegate);
9577
conventionBuilder.WithDisplayName("Fallback " + pattern);

src/Http/Routing/src/Builder/RoutingBuilderExtensions.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ public static class RoutingBuilderExtensions
1919
/// <returns>A reference to this instance after the operation has completed.</returns>
2020
public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IRouter router)
2121
{
22-
if (builder == null)
23-
{
24-
throw new ArgumentNullException(nameof(builder));
25-
}
26-
27-
if (router == null)
28-
{
29-
throw new ArgumentNullException(nameof(router));
30-
}
22+
ArgumentNullException.ThrowIfNull(builder);
23+
ArgumentNullException.ThrowIfNull(router);
3124

3225
if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
3326
{
@@ -49,15 +42,8 @@ public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IR
4942
/// <returns>A reference to this instance after the operation has completed.</returns>
5043
public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, Action<IRouteBuilder> action)
5144
{
52-
if (builder == null)
53-
{
54-
throw new ArgumentNullException(nameof(builder));
55-
}
56-
57-
if (action == null)
58-
{
59-
throw new ArgumentNullException(nameof(action));
60-
}
45+
ArgumentNullException.ThrowIfNull(builder);
46+
ArgumentNullException.ThrowIfNull(action);
6147

6248
if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
6349
{

src/Http/Routing/src/Builder/RoutingEndpointConventionBuilderExtensions.cs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,8 @@ public static class RoutingEndpointConventionBuilderExtensions
2222
/// <returns>A reference to this instance after the operation has completed.</returns>
2323
public static TBuilder RequireHost<TBuilder>(this TBuilder builder, params string[] hosts) where TBuilder : IEndpointConventionBuilder
2424
{
25-
if (builder == null)
26-
{
27-
throw new ArgumentNullException(nameof(builder));
28-
}
29-
30-
if (hosts == null)
31-
{
32-
throw new ArgumentNullException(nameof(hosts));
33-
}
25+
ArgumentNullException.ThrowIfNull(builder);
26+
ArgumentNullException.ThrowIfNull(hosts);
3427

3528
builder.Add(endpointBuilder =>
3629
{
@@ -48,10 +41,7 @@ public static TBuilder RequireHost<TBuilder>(this TBuilder builder, params strin
4841
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
4942
public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, string displayName) where TBuilder : IEndpointConventionBuilder
5043
{
51-
if (builder == null)
52-
{
53-
throw new ArgumentNullException(nameof(builder));
54-
}
44+
ArgumentNullException.ThrowIfNull(builder);
5545

5646
builder.Add(b =>
5747
{
@@ -70,15 +60,8 @@ public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, string d
7060
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
7161
public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, Func<EndpointBuilder, string> func) where TBuilder : IEndpointConventionBuilder
7262
{
73-
if (builder == null)
74-
{
75-
throw new ArgumentNullException(nameof(builder));
76-
}
77-
78-
if (func == null)
79-
{
80-
throw new ArgumentNullException(nameof(func));
81-
}
63+
ArgumentNullException.ThrowIfNull(builder);
64+
ArgumentNullException.ThrowIfNull(func);
8265

8366
builder.Add(b =>
8467
{
@@ -97,15 +80,8 @@ public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, Func<End
9780
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
9881
public static TBuilder WithMetadata<TBuilder>(this TBuilder builder, params object[] items) where TBuilder : IEndpointConventionBuilder
9982
{
100-
if (builder == null)
101-
{
102-
throw new ArgumentNullException(nameof(builder));
103-
}
104-
105-
if (items == null)
106-
{
107-
throw new ArgumentNullException(nameof(items));
108-
}
83+
ArgumentNullException.ThrowIfNull(builder);
84+
ArgumentNullException.ThrowIfNull(items);
10985

11086
builder.Add(b =>
11187
{

0 commit comments

Comments
 (0)