-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
285 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
namespace Fluxera.DomainEvents.Abstractions | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
using JetBrains.Annotations; | ||
|
||
internal static class Guard | ||
{ | ||
public static T ThrowIfNull<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(argument, parameterName); | ||
|
||
return argument; | ||
} | ||
|
||
public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
if(string.IsNullOrEmpty(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be empty.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
if(string.IsNullOrWhiteSpace(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be whitespace-only.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static bool ThrowIfFalse(bool argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null, string message = null) | ||
{ | ||
if(!argument) | ||
{ | ||
throw new ArgumentException(message ?? "Value cannot be false.", parameterName); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static IEnumerable<T> ThrowIfNullOrEmpty<T>(IEnumerable<T> argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
// ReSharper disable PossibleMultipleEnumeration | ||
if(!argument.Any()) | ||
{ | ||
throw new ArgumentException("Enumerable cannot be empty.", parameterName); | ||
} | ||
|
||
return argument; | ||
// ReSharper enable PossibleMultipleEnumeration | ||
} | ||
|
||
#if NET7_0_OR_GREATER | ||
public static T ThrowIfNegative<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
where T : INumber<T> | ||
{ | ||
if(T.IsNegative(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
#endif | ||
|
||
#if NET6_0 | ||
public static byte ThrowIfNegative(byte argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static short ThrowIfNegative(short argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static int ThrowIfNegative(int argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static long ThrowIfNegative(long argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#if NET6_0 | ||
namespace Fluxera.Entity | ||
{ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
|
||
internal static class EnumerableExtensions | ||
{ | ||
/// <summary> | ||
/// Converts the enumerable to a read-only collection. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements.</typeparam> | ||
/// <param name="enumerable">The collection to write-protect.</param> | ||
/// <returns>A write-protected collection.</returns> | ||
public static IReadOnlyCollection<T> AsReadOnly<T>(this IEnumerable<T> enumerable) | ||
{ | ||
enumerable = Guard.ThrowIfNull(enumerable); | ||
|
||
return new ReadOnlyCollection<T>(enumerable.ToList()); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.