namespace Kit.Helpers { using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; /// /// a static class that checks if parameter is invalid and raises an exception in this case /// public static class Check { [DebuggerStepThrough] public static void ArgumentIsNotNull([AllowNull][NotNull] object argument, string name) { if (argument == null) { throw new ArgumentNullException(name); } } [DebuggerStepThrough] public static void ArgumentIsNotNull([AllowNull][NotNull] object argument, string name, string message) { if (argument == null) { throw new ArgumentException(message, name); } } [DebuggerStepThrough] public static void ArgumentIsNotNullOrEmpty([AllowNull][NotNull] string argument, string name) { if (string.IsNullOrWhiteSpace(argument)) { throw new ArgumentNullException(name); } } [DebuggerStepThrough] public static void ArgumentIsNotNullOrEmpty([AllowNull][NotNull] string argument, string name, string message) { if (string.IsNullOrWhiteSpace(argument)) { throw new ArgumentException(message, name); } } [DebuggerStepThrough] public static void IntegerArgumentPositive(Int32 value, string name) { if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty); } [DebuggerStepThrough] public static void IntegerArgumentPositive(Int32? value, string name) { if (!value.HasValue) throw new ArgumentNullException(name); if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty); } [DebuggerStepThrough] public static void IntegerPositive(Int16 value, string message) { if (value < 1) throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IntegerPositive(Int32 value, string message) { if (value < 1) throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IntegerPositive(Int64 value, string message) { if (value < 1) throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IntegerPositive(Int32? value, string message) { if (!value.HasValue) throw new InvalidOperationException(message); if (value.Value < 1) throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IntegerArgumentPositive(Int64 value, string name) { if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty); } [DebuggerStepThrough] public static void IntegerArgumentPositive(Int64? value, string name) { if (!value.HasValue) throw new ArgumentNullException(name); if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty); } [DebuggerStepThrough] public static void StringArgumentIsNotNullOrEmpty(string str, string? paramName, string? message) { if (string.IsNullOrEmpty(str)) throw new ArgumentNullException(paramName: paramName, message: message); str = str.Trim(); if (string.IsNullOrEmpty(str)) throw new ArgumentNullException(paramName: paramName, message: message); } [DebuggerStepThrough] public static void IsModelStateValid(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary modelState, string? message = null) { if (modelState.IsValid) return; if (string.IsNullOrWhiteSpace(message) == false) { throw new InvalidOperationException(message); } var allErrors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(); throw new InvalidOperationException(allErrors.Join(Environment.NewLine)); } [DebuggerStepThrough] public static void IsTrue(bool argument, string message) { if (argument == false) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNotTrue(bool argument, string message) { if (argument == true) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNull([AllowNull][NotNull] object argument, string message) { if (argument != null) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNotNull([AllowNull][NotNull] object argument, string message) { if (argument == null) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNullOrEmpty(Guid? argument, string message) { if (argument.HasValue == false || argument.Value.Equals(Guid.Empty)) { return; } throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IsEmpty(Guid argument, string message) { if (argument.Equals(Guid.Empty)) { return; } throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IsNotNullOrEmpty([AllowNull][NotNull]Guid? argument, string message) { if (argument.HasValue && argument.Value.Equals(Guid.Empty) == false) { return; } throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IsNotEmpty(Guid argument, string message) { if (argument.Equals(Guid.Empty) == false) { return; } throw new InvalidOperationException(message); } [DebuggerStepThrough] public static void IsNullOrEmpty([AllowNull][NotNull] IEnumerable argument, string message) { if (argument.IsNullOrEmpty() == false) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNotNullOrEmpty([AllowNull][NotNull] IEnumerable argument, string message) { if (argument.IsNullOrEmpty()) { throw new InvalidOperationException(message); } } /// /// Checks whether argument is of specified type /// Throws InvalidOperationException if argument is of wrong type /// /// the argument to be checked /// the Type /// Implied to contain a {0} placeholder within in order to be replaced with string representation of the type param [DebuggerStepThrough] public static void ArgumentIsOfType(object? argument, Type type, string message) { Check.ArgumentIsNotNull(argument, "argument"); Check.ArgumentIsNotNull(type, "type"); if (type != argument.GetType()) { throw new InvalidOperationException(string.Format(message, type.ToString())); } } [DebuggerStepThrough] public static void IsNotNullOrWhiteSpace([AllowNull][NotNull] string? value, string message) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidOperationException(message); } } [DebuggerStepThrough] public static void IsNullOrWhiteSpace([AllowNull][NotNull] string? value, string message) { if (string.IsNullOrWhiteSpace(value) == false) { throw new InvalidOperationException(message); } } } }