Skip to content

Refactor "Routing/Builder" methods pre conditions #41783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 15 additions & 61 deletions src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static GroupRouteBuilder MapGroup(this IEndpointRouteBuilder endpoints, s
/// </returns>
public static GroupRouteBuilder MapGroup(this IEndpointRouteBuilder endpoints, RoutePattern prefix)
{
ArgumentNullException.ThrowIfNull(endpoints, nameof(endpoints));
ArgumentNullException.ThrowIfNull(prefix, nameof(prefix));
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(prefix);

return new(endpoints, prefix);
}
Expand Down Expand Up @@ -158,10 +158,7 @@ public static IEndpointConventionBuilder MapMethods(
IEnumerable<string> httpMethods,
RequestDelegate requestDelegate)
{
if (httpMethods == null)
{
throw new ArgumentNullException(nameof(httpMethods));
}
ArgumentNullException.ThrowIfNull(httpMethods);

var builder = endpoints.Map(RoutePatternFactory.Parse(pattern), requestDelegate);
builder.WithDisplayName($"{pattern} HTTP: {string.Join(", ", httpMethods)}");
Expand Down Expand Up @@ -198,20 +195,9 @@ public static IEndpointConventionBuilder Map(
RoutePattern pattern,
RequestDelegate requestDelegate)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}

if (requestDelegate == null)
{
throw new ArgumentNullException(nameof(requestDelegate));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(requestDelegate);

const int defaultOrder = 0;

Expand Down Expand Up @@ -346,10 +332,7 @@ public static RouteHandlerBuilder MapMethods(
IEnumerable<string> httpMethods,
Delegate handler)
{
if (httpMethods is null)
{
throw new ArgumentNullException(nameof(httpMethods));
}
ArgumentNullException.ThrowIfNull(httpMethods);

var disableInferredBody = false;
foreach (var method in httpMethods)
Expand Down Expand Up @@ -437,15 +420,8 @@ public static RouteHandlerBuilder Map(
[RequiresUnreferencedCode(MapEndpointTrimmerWarning)]
public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoints, Delegate handler)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(handler);

return endpoints.MapFallback("{*path:nonfile}", handler);
}
Expand Down Expand Up @@ -477,20 +453,9 @@ public static RouteHandlerBuilder MapFallback(
string pattern,
Delegate handler)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(handler);

var conventionBuilder = endpoints.Map(pattern, handler);
conventionBuilder.WithDisplayName("Fallback " + pattern);
Expand All @@ -506,20 +471,9 @@ private static RouteHandlerBuilder Map(
bool disableInferBodyFromParameters,
IEnumerable<object>? initialEndpointMetadata = null)
{
if (endpoints is null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (pattern is null)
{
throw new ArgumentNullException(nameof(pattern));
}

if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(handler);

const int defaultOrder = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public static class EndpointRoutingApplicationBuilderExtensions
/// </remarks>
public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
ArgumentNullException.ThrowIfNull(builder);

VerifyRoutingServicesAreRegistered(builder);

Expand Down Expand Up @@ -85,15 +82,8 @@ public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
/// </remarks>
public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configure);

VerifyRoutingServicesAreRegistered(builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ public static class FallbackEndpointRouteBuilderExtensions
/// </remarks>
public static IEndpointConventionBuilder MapFallback(this IEndpointRouteBuilder endpoints, RequestDelegate requestDelegate)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (requestDelegate == null)
{
throw new ArgumentNullException(nameof(requestDelegate));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(requestDelegate);

return endpoints.MapFallback("{*path:nonfile}", requestDelegate);
}
Expand Down Expand Up @@ -76,20 +69,9 @@ public static IEndpointConventionBuilder MapFallback(
string pattern,
RequestDelegate requestDelegate)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}

if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}

if (requestDelegate == null)
{
throw new ArgumentNullException(nameof(requestDelegate));
}
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(requestDelegate);

var conventionBuilder = endpoints.Map(pattern, requestDelegate);
conventionBuilder.WithDisplayName("Fallback " + pattern);
Expand Down
22 changes: 4 additions & 18 deletions src/Http/Routing/src/Builder/RoutingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ public static class RoutingBuilderExtensions
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IRouter router)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (router == null)
{
throw new ArgumentNullException(nameof(router));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(router);

if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
{
Expand All @@ -49,15 +42,8 @@ public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IR
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, Action<IRouteBuilder> action)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(action);

if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ public static class RoutingEndpointConventionBuilderExtensions
/// <returns>A reference to this instance after the operation has completed.</returns>
public static TBuilder RequireHost<TBuilder>(this TBuilder builder, params string[] hosts) where TBuilder : IEndpointConventionBuilder
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (hosts == null)
{
throw new ArgumentNullException(nameof(hosts));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(hosts);

builder.Add(endpointBuilder =>
{
Expand All @@ -48,10 +41,7 @@ public static TBuilder RequireHost<TBuilder>(this TBuilder builder, params strin
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, string displayName) where TBuilder : IEndpointConventionBuilder
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
ArgumentNullException.ThrowIfNull(builder);

builder.Add(b =>
{
Expand All @@ -70,15 +60,8 @@ public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, string d
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, Func<EndpointBuilder, string> func) where TBuilder : IEndpointConventionBuilder
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (func == null)
{
throw new ArgumentNullException(nameof(func));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(func);

builder.Add(b =>
{
Expand All @@ -97,15 +80,8 @@ public static TBuilder WithDisplayName<TBuilder>(this TBuilder builder, Func<End
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
public static TBuilder WithMetadata<TBuilder>(this TBuilder builder, params object[] items) where TBuilder : IEndpointConventionBuilder
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(items);

builder.Add(b =>
{
Expand Down