Skip to content

Commit a6d59a4

Browse files
Use ThrowIfNull and ThrowIfNullOrEmpty everywhere
1 parent 717c7ac commit a6d59a4

File tree

161 files changed

+589
-1291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+589
-1291
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersion.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected ApiVersion( double version, string? status, Func<string?, bool> isVali
7474

7575
Status = ValidateStatus(
7676
status,
77-
isValidStatus ?? throw new ArgumentNullException( nameof( isValidStatus ) ) );
77+
isValidStatus ?? throw new System.ArgumentNullException( nameof( isValidStatus ) ) );
7878

7979
var number = new decimal( version );
8080
var bits = decimal.GetBits( number );
@@ -124,10 +124,7 @@ protected internal ApiVersion(
124124
/// <param name="other">The instance to derive from.</param>
125125
protected ApiVersion( ApiVersion other )
126126
{
127-
if ( other == null )
128-
{
129-
throw new ArgumentNullException( nameof( other ) );
130-
}
127+
ArgumentNullException.ThrowIfNull( other );
131128

132129
hashCode = other.hashCode;
133130
GroupVersion = other.GroupVersion;
@@ -355,7 +352,7 @@ public virtual string ToString( string? format, IFormatProvider? formatProvider
355352
return status;
356353
}
357354

358-
var message = string.Format( CultureInfo.CurrentCulture, SR.ApiVersionBadStatus, status );
355+
var message = string.Format( CultureInfo.CurrentCulture, Format.ApiVersionBadStatus, status );
359356
throw new ArgumentException( message, nameof( status ) );
360357
}
361358
}

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersionFormatProvider.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public ApiVersionFormatProvider()
200200
/// </summary>
201201
/// <param name="dateTimeFormat">The <see cref="DateTimeFormatInfo"/> used by the format provider.</param>
202202
public ApiVersionFormatProvider( DateTimeFormatInfo dateTimeFormat )
203-
: this( dateTimeFormat ?? throw new ArgumentNullException( nameof( dateTimeFormat ) ), dateTimeFormat.Calendar ) { }
203+
: this( dateTimeFormat ?? throw new System.ArgumentNullException( nameof( dateTimeFormat ) ), dateTimeFormat.Calendar ) { }
204204

205205
/// <summary>
206206
/// Initializes a new instance of the <see cref="ApiVersionFormatProvider"/> class.
@@ -293,20 +293,11 @@ protected virtual void FormatVersionPart(
293293
Text format,
294294
IFormatProvider formatProvider )
295295
{
296-
if ( text == null )
297-
{
298-
throw new ArgumentNullException( nameof( text ) );
299-
}
300-
301-
if ( apiVersion == null )
302-
{
303-
throw new ArgumentNullException( nameof( apiVersion ) );
304-
}
305-
306-
if ( Str.IsNullOrEmpty( format ) )
307-
{
308-
throw new ArgumentNullException( nameof( format ) );
309-
}
296+
ArgumentNullException.ThrowIfNull( text );
297+
ArgumentNullException.ThrowIfNull( apiVersion );
298+
#if NETSTANDARD1_0
299+
ArgumentNullException.ThrowIfNull( format );
300+
#endif
310301

311302
switch ( format[0] )
312303
{
@@ -334,16 +325,8 @@ protected virtual void FormatStatusPart(
334325
Text format,
335326
IFormatProvider formatProvider )
336327
{
337-
if ( text == null )
338-
{
339-
throw new ArgumentNullException( nameof( text ) );
340-
}
341-
342-
if ( apiVersion == null )
343-
{
344-
throw new ArgumentNullException( nameof( apiVersion ) );
345-
}
346-
328+
ArgumentNullException.ThrowIfNull( text );
329+
ArgumentNullException.ThrowIfNull( apiVersion );
347330
text.Append( apiVersion.Status );
348331
}
349332

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersionMetadata.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public class ApiVersionMetadata
3434
/// <param name="other">The other <see cref="ApiVersionMetadata">instance</see> to initialize from.</param>
3535
protected ApiVersionMetadata( ApiVersionMetadata other )
3636
{
37-
if ( other == null )
38-
{
39-
throw new ArgumentNullException( nameof( other ) );
40-
}
37+
ArgumentNullException.ThrowIfNull( other );
4138

4239
apiModel = other.apiModel;
4340
endpointModel = other.endpointModel;

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersionModelExtensions.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@ public static class ApiVersionModelExtensions
1717
/// <paramref name="otherVersion">other version information</paramref> and the current version information.</returns>
1818
public static ApiVersionModel Aggregate( this ApiVersionModel version, ApiVersionModel otherVersion )
1919
{
20-
if ( version == null )
21-
{
22-
throw new ArgumentNullException( nameof( version ) );
23-
}
24-
25-
if ( otherVersion == null )
26-
{
27-
throw new ArgumentNullException( nameof( otherVersion ) );
28-
}
20+
ArgumentNullException.ThrowIfNull( version );
21+
ArgumentNullException.ThrowIfNull( otherVersion );
2922

3023
var implemented = new SortedSet<ApiVersion>( version.ImplementedApiVersions );
3124
var supported = new SortedSet<ApiVersion>( version.SupportedApiVersions );
@@ -50,15 +43,8 @@ public static ApiVersionModel Aggregate( this ApiVersionModel version, ApiVersio
5043
/// <paramref name="otherVersions">other version information</paramref> and the current version information.</returns>
5144
public static ApiVersionModel Aggregate( this ApiVersionModel version, IEnumerable<ApiVersionModel> otherVersions )
5245
{
53-
if ( version == null )
54-
{
55-
throw new ArgumentNullException( nameof( version ) );
56-
}
57-
58-
if ( otherVersions == null )
59-
{
60-
throw new ArgumentNullException( nameof( otherVersions ) );
61-
}
46+
ArgumentNullException.ThrowIfNull( version );
47+
ArgumentNullException.ThrowIfNull( otherVersions );
6248

6349
if ( ( otherVersions is ICollection<ApiVersionModel> collection && collection.Count == 0 ) ||
6450
( otherVersions is IReadOnlyCollection<ApiVersionModel> readOnlyCollection && readOnlyCollection.Count == 0 ) )
@@ -99,10 +85,7 @@ public static ApiVersionModel Aggregate( this ApiVersionModel version, IEnumerab
9985
/// <returns>A new <see cref="ApiVersionModel"/> that is the aggregated result of the provided <paramref name="versions">version information</paramref>.</returns>
10086
public static ApiVersionModel Aggregate( this IEnumerable<ApiVersionModel> versions )
10187
{
102-
if ( versions == null )
103-
{
104-
throw new ArgumentNullException( nameof( versions ) );
105-
}
88+
ArgumentNullException.ThrowIfNull( versions );
10689

10790
if ( ( versions is ICollection<ApiVersionModel> collection && collection.Count == 0 ) ||
10891
( versions is IReadOnlyCollection<ApiVersionModel> readOnlyCollection && readOnlyCollection.Count == 0 ) )

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersionsBaseAttribute.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected ApiVersionsBaseAttribute( string version, params string[] otherVersion
9696
/// <param name="parser">The parser used to parse the specified versions.</param>
9797
/// <param name="version">The API version string.</param>
9898
protected ApiVersionsBaseAttribute( IApiVersionParser parser, string version ) =>
99-
Versions = new[] { ( parser ?? throw new ArgumentNullException( nameof( parser ) ) ).Parse( version ) };
99+
Versions = new[] { ( parser ?? throw new System.ArgumentNullException( nameof( parser ) ) ).Parse( version ) };
100100

101101
/// <summary>
102102
/// Initializes a new instance of the <see cref="ApiVersionsBaseAttribute"/> class.
@@ -106,10 +106,7 @@ protected ApiVersionsBaseAttribute( IApiVersionParser parser, string version ) =
106106
/// <param name="otherVersions">An array of API other version strings.</param>
107107
protected ApiVersionsBaseAttribute( IApiVersionParser parser, string version, params string[] otherVersions )
108108
{
109-
if ( parser == null )
110-
{
111-
throw new ArgumentNullException( nameof( parser ) );
112-
}
109+
ArgumentNullException.ThrowIfNull( parser );
113110

114111
int count;
115112

src/Abstractions/src/Asp.Versioning.Abstractions/Conventions/ApiVersionConventionBuilderBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ protected virtual void MergeAttributesWithConventions( IEnumerable<object> attri
7272
/// <param name="attributes">The <see cref="IReadOnlyList{T}">read-only list</see> of attributes to merge.</param>
7373
protected virtual void MergeAttributesWithConventions( IReadOnlyList<object> attributes )
7474
{
75-
if ( attributes == null )
76-
{
77-
throw new ArgumentNullException( nameof( attributes ) );
78-
}
75+
ArgumentNullException.ThrowIfNull( attributes );
7976

8077
if ( VersionNeutral )
8178
{

src/Abstractions/src/Asp.Versioning.Abstractions/Conventions/ApiVersionConventionBuilderExtensions.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ public static class ApiVersionConventionBuilderExtensions
8484
public static T HasApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVersions )
8585
where T : notnull, IDeclareApiVersionConventionBuilder
8686
{
87-
if ( apiVersions == null )
88-
{
89-
throw new ArgumentNullException( nameof( apiVersions ) );
90-
}
87+
ArgumentNullException.ThrowIfNull( apiVersions );
9188

9289
foreach ( var apiVersion in apiVersions )
9390
{
@@ -170,10 +167,7 @@ public static T HasApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVe
170167
public static T HasDeprecatedApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVersions )
171168
where T : notnull, IDeclareApiVersionConventionBuilder
172169
{
173-
if ( apiVersions == null )
174-
{
175-
throw new ArgumentNullException( nameof( apiVersions ) );
176-
}
170+
ArgumentNullException.ThrowIfNull( apiVersions );
177171

178172
foreach ( var apiVersion in apiVersions )
179173
{
@@ -256,10 +250,7 @@ public static T HasDeprecatedApiVersions<T>( this T builder, IEnumerable<ApiVers
256250
public static T AdvertisesApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVersions )
257251
where T : notnull, IDeclareApiVersionConventionBuilder
258252
{
259-
if ( apiVersions == null )
260-
{
261-
throw new ArgumentNullException( nameof( apiVersions ) );
262-
}
253+
ArgumentNullException.ThrowIfNull( apiVersions );
263254

264255
foreach ( var apiVersion in apiVersions )
265256
{
@@ -342,10 +333,7 @@ public static T AdvertisesApiVersions<T>( this T builder, IEnumerable<ApiVersion
342333
public static T AdvertisesDeprecatedApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVersions )
343334
where T : notnull, IDeclareApiVersionConventionBuilder
344335
{
345-
if ( apiVersions == null )
346-
{
347-
throw new ArgumentNullException( nameof( apiVersions ) );
348-
}
336+
ArgumentNullException.ThrowIfNull( apiVersions );
349337

350338
foreach ( var apiVersion in apiVersions )
351339
{
@@ -428,10 +416,7 @@ public static T AdvertisesDeprecatedApiVersions<T>( this T builder, IEnumerable<
428416
public static T MapToApiVersions<T>( this T builder, IEnumerable<ApiVersion> apiVersions )
429417
where T : notnull, IMapToApiVersionConventionBuilder
430418
{
431-
if ( apiVersions == null )
432-
{
433-
throw new ArgumentNullException( nameof( apiVersions ) );
434-
}
419+
ArgumentNullException.ThrowIfNull( apiVersions );
435420

436421
foreach ( var apiVersion in apiVersions )
437422
{

src/Abstractions/src/Asp.Versioning.Abstractions/IApiVersionParameterSourceExtensions.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public static class IApiVersionParameterSourceExtensions
1818
/// <returns>True if the parameter source versions by query string; otherwise, false.</returns>
1919
public static bool VersionsByQueryString( this IApiVersionParameterSource source, bool allowMultipleLocations = true )
2020
{
21-
if ( source == null )
22-
{
23-
throw new ArgumentNullException( nameof( source ) );
24-
}
21+
ArgumentNullException.ThrowIfNull( source );
2522

2623
var context = new DescriptionContext( Query );
2724

@@ -39,10 +36,7 @@ public static bool VersionsByQueryString( this IApiVersionParameterSource source
3936
/// <returns>True if the parameter source versions by HTTP header; otherwise, false.</returns>
4037
public static bool VersionsByHeader( this IApiVersionParameterSource source, bool allowMultipleLocations = true )
4138
{
42-
if ( source == null )
43-
{
44-
throw new ArgumentNullException( nameof( source ) );
45-
}
39+
ArgumentNullException.ThrowIfNull( source );
4640

4741
var context = new DescriptionContext( Header );
4842

@@ -60,10 +54,7 @@ public static bool VersionsByHeader( this IApiVersionParameterSource source, boo
6054
/// <returns>True if the parameter source versions by URL path segment; otherwise, false.</returns>
6155
public static bool VersionsByUrl( this IApiVersionParameterSource source, bool allowMultipleLocations = true )
6256
{
63-
if ( source == null )
64-
{
65-
throw new ArgumentNullException( nameof( source ) );
66-
}
57+
ArgumentNullException.ThrowIfNull( source );
6758

6859
var context = new DescriptionContext( Path );
6960

@@ -81,10 +72,7 @@ public static bool VersionsByUrl( this IApiVersionParameterSource source, bool a
8172
/// <returns>True if the parameter source versions by media type; otherwise, false.</returns>
8273
public static bool VersionsByMediaType( this IApiVersionParameterSource source, bool allowMultipleLocations = true )
8374
{
84-
if ( source == null )
85-
{
86-
throw new ArgumentNullException( nameof( source ) );
87-
}
75+
ArgumentNullException.ThrowIfNull( source );
8876

8977
var context = new DescriptionContext( MediaTypeParameter );
9078

@@ -102,10 +90,7 @@ public static bool VersionsByMediaType( this IApiVersionParameterSource source,
10290
/// <paramref name="location"/> or <c>null</c>.</returns>
10391
public static string GetParameterName( this IApiVersionParameterSource source, ApiVersionParameterLocation location )
10492
{
105-
if ( source == null )
106-
{
107-
throw new ArgumentNullException( nameof( source ) );
108-
}
93+
ArgumentNullException.ThrowIfNull( source );
10994

11095
var context = new DescriptionContext( location );
11196

@@ -122,10 +107,7 @@ public static string GetParameterName( this IApiVersionParameterSource source, A
122107
/// <returns>The names of the parameters defined by the parameter source for the specified <paramref name="location"/>.</returns>
123108
public static IReadOnlyList<string> GetParameterNames( this IApiVersionParameterSource source, ApiVersionParameterLocation location )
124109
{
125-
if ( source == null )
126-
{
127-
throw new ArgumentNullException( nameof( source ) );
128-
}
110+
ArgumentNullException.ThrowIfNull( source );
129111

130112
var context = new DescriptionContext( location );
131113

0 commit comments

Comments
 (0)