diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index fd336c6..b471ac7 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -67,4 +67,12 @@
### **v.1.0.8.0638**
-> Added string extension `GetHashSha512String`, `FromSpaceSeparatedString`, `IsMissing`, `IsNullOrEmpty`, `AddQueryString`, `AddHashFragment`, `GetOrigin`, `Obfuscate`.
--> Added Enumerable extension `ToSpaceSeparatedString`, `HasDuplicates`, `GetDuplicates`.
\ No newline at end of file
+-> Added Enumerable extension `ToSpaceSeparatedString`, `HasDuplicates`, `GetDuplicates`.
+
+### **v.1.0.9.2108**
+-> Added object extensions `ThrowIfArgNull`, `ThrowArgIfNull`.
+-> Added bool extensions `IsTrue`, `IsFalse`.
+-> Added null check extensions `IsNotNull`, `IsDbNull`.
+-> Added string extensions `ThrowArgIfNull`, `ThrowArgIfNullOrEmpty`, `ThrowIfArgNull`, `ThrowIfArgNullOrEmpty`.
+-> Adjust validation for input params at some methods.
+-> Small code refactor.
\ No newline at end of file
diff --git a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs
index df3aed8..6cf8332 100644
--- a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs
+++ b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs
@@ -57,7 +57,7 @@ public static IEnumerable Replace(this IEnumerable enumerable, int inde
///
public static string Join(this IEnumerable source, string separator)
{
- return source == null ? string.Empty : string.Join(separator, source);
+ return source.IsNull() ? string.Empty : string.Join(separator, source);
}
///
diff --git a/src/DomainCommonExtensions/CommonExtensions/DocumentationExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/DocumentationExtensions.cs
index f5cc03c..0ca9388 100644
--- a/src/DomainCommonExtensions/CommonExtensions/DocumentationExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/DocumentationExtensions.cs
@@ -72,7 +72,7 @@ public static XmlElement GetDocumentation(this MethodInfo methodInfo)
/// The XML fragment describing the member
public static XmlElement GetDocumentation(this MemberInfo memberInfo)
{
- if (memberInfo .IsNull()) return null;
+ if (memberInfo.IsNull()) return null;
// First character [0] of member type is prefix character in the name in the XML
return XmlFromName(memberInfo.DeclaringType, memberInfo.MemberType.ToString()[0], memberInfo.Name);
@@ -89,7 +89,7 @@ public static string GetSummary(this MemberInfo memberInfo)
var summaryElm = element?.SelectSingleNode("summary");
if (summaryElm.IsNull()) return "";
- return summaryElm.InnerText.Trim();
+ return summaryElm?.InnerText.Trim();
}
///
@@ -112,9 +112,9 @@ public static string GetSummary(this Type type)
{
var element = type.GetDocumentation();
var summaryElm = element?.SelectSingleNode("summary");
- if (summaryElm .IsNull()) return "";
+ if (summaryElm.IsNull()) return "";
- return summaryElm.InnerText.Trim();
+ return summaryElm?.InnerText.Trim();
}
///
@@ -129,7 +129,7 @@ private static XmlElement XmlFromName(this Type type, char prefix, string name)
{
string fullName;
- if (string.IsNullOrEmpty(name))
+ if (name.IsNullOrEmpty())
fullName = prefix + ":" + type.FullName;
else
fullName = prefix + ":" + type.FullName + "." + name;
diff --git a/src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs
index fef1caf..b551927 100644
--- a/src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs
@@ -22,6 +22,7 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
+using DomainCommonExtensions.DataTypeExtensions;
#endregion
@@ -37,6 +38,9 @@ public static partial class CryptoExtensions
///
public static string AesEncryptString(this string plainText, string key)
{
+ key.ThrowIfArgNullOrEmpty(nameof(key));
+ plainText.ThrowIfArgNull(nameof(plainText));
+
var iv = new byte[16];
byte[] array;
@@ -72,6 +76,9 @@ public static string AesEncryptString(this string plainText, string key)
///
public static string AesDecryptString(this string cipherText, string key)
{
+ key.ThrowIfArgNullOrEmpty(nameof(key));
+ cipherText.ThrowIfArgNullOrEmpty(nameof(cipherText));
+
var iv = new byte[16];
var buffer = Convert.FromBase64String(cipherText);
diff --git a/src/DomainCommonExtensions/CommonExtensions/Encryption/RSAEncryptionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/Encryption/RSAEncryptionExtensions.cs
index 8346d30..cd9446a 100644
--- a/src/DomainCommonExtensions/CommonExtensions/Encryption/RSAEncryptionExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/Encryption/RSAEncryptionExtensions.cs
@@ -18,6 +18,8 @@
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
+using DomainCommonExtensions.DataTypeExtensions;
+
// ReSharper disable CheckNamespace
namespace DomainCommonExtensions.CommonExtensions
@@ -34,11 +36,8 @@ public static partial class CryptoExtensions
// ReSharper disable once InconsistentNaming
public static string EncryptWithRSA(this string stringToEncrypt, string key)
{
- if (string.IsNullOrEmpty(stringToEncrypt))
- throw new ArgumentException("An empty string value cannot be encrypted.");
-
- if (string.IsNullOrEmpty(key))
- throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
+ stringToEncrypt.ThrowArgIfNullOrEmpty("An empty string value cannot be encrypted.");
+ key.ThrowArgIfNullOrEmpty("Cannot encrypt using an empty key. Please supply an encryption key.");
var cspp = new CspParameters { KeyContainerName = key };
var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true };
@@ -58,12 +57,8 @@ public static string EncryptWithRSA(this string stringToEncrypt, string key)
public static string DecryptWithRSA(this string stringToDecrypt, string key)
{
string result = null;
-
- if (string.IsNullOrEmpty(stringToDecrypt))
- throw new ArgumentException("An empty string value cannot be encrypted.");
-
- if (string.IsNullOrEmpty(key))
- throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
+ stringToDecrypt.ThrowArgIfNullOrEmpty("An empty string value cannot be decrypted.");
+ key.ThrowArgIfNullOrEmpty("Cannot decrypt using an empty key. Please supply a decryption key.");
try
{
diff --git a/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs
index 262524a..8acdfe3 100644
--- a/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs
@@ -42,18 +42,18 @@ public static string GetFullError(this Exception ex, bool showCallStack = true)
{
if (!showCallStack)
{
- if (ex.Message != "An error occurred while updating the entries. See the inner exception for details.")
- result.AppendLine(ex.Message);
+ if (ex?.Message != "An error occurred while updating the entries. See the inner exception for details.")
+ result.AppendLine(ex?.Message);
}
else
- result.AppendLine(ex.Message);
+ result.AppendLine(ex?.Message);
if (showCallStack)
- result.Append(ex.StackTrace);
+ result.Append(ex?.StackTrace);
ex = ex.InnerException;
}
- while (ex != null);
+ while (ex.IsNotNull());
return result?.ToString().TrimIfNotNull();
}
diff --git a/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs
index 59ae702..8f807cc 100644
--- a/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs
@@ -72,6 +72,7 @@ public static void UpdateValue(this ExpandoObject expando, string propertyName,
public static object GetValue(this ExpandoObject expando, string key)
{
object r = expando.FirstOrDefault(x => x.Key == key).Value;
+
return r;
}
}
diff --git a/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs
index e0859fe..fbb1850 100644
--- a/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs
@@ -16,6 +16,7 @@
#region U S A G E S
+using System;
using System.Collections.Generic;
#endregion
@@ -31,12 +32,10 @@ public static class NullExtensions
///
/// Is null
///
- ///
+ /// Object to be checked
///
public static bool IsNull(this object obj)
- {
- return obj == null;
- }
+ => obj == null;
///
/// Check if KeyValue is null
@@ -49,5 +48,21 @@ public static bool IsNull(this KeyValuePair source)
{
return source.Equals(default(KeyValuePair));
}
+
+ ///
+ /// Is not null
+ ///
+ /// Object to be checked
+ ///
+ public static bool IsNotNull(this object obj)
+ => obj != null;
+
+ ///
+ /// Is if source object is DBNull
+ ///
+ /// Object to be checked
+ ///
+ public static bool IsDbNull(this object obj)
+ => obj == DBNull.Value;
}
}
\ No newline at end of file
diff --git a/src/DomainCommonExtensions/CommonExtensions/PredicateBuilderExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/PredicateBuilderExtensions.cs
index e8dd530..b9bb650 100644
--- a/src/DomainCommonExtensions/CommonExtensions/PredicateBuilderExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/PredicateBuilderExtensions.cs
@@ -63,6 +63,7 @@ public static Expression> Or(this Expression> exp
Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters);
+
return Expression.Lambda>
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}
@@ -79,6 +80,7 @@ public static Expression> And(this Expression> ex
Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters);
+
return Expression.Lambda>
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
diff --git a/src/DomainCommonExtensions/CommonExtensions/ReflectionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ReflectionExtensions.cs
index 5045e4a..9a7a089 100644
--- a/src/DomainCommonExtensions/CommonExtensions/ReflectionExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/ReflectionExtensions.cs
@@ -37,7 +37,7 @@ public static class ReflectionExtensions
public static void CopyProperties(this object source, object destination)
{
//if any this null throw an exception
- if (source == null || destination == null)
+ if (source.IsNull() || destination.IsNull())
throw new Exception("Source or/and Destination Objects are null");
var typeDest = destination.GetType();
@@ -50,7 +50,7 @@ public static void CopyProperties(this object source, object destination)
if (!srcProp.CanRead)
continue;
var targetProperty = typeDest.GetProperty(srcProp.Name);
- if (targetProperty == null)
+ if (targetProperty.IsNull())
continue;
if (!targetProperty.CanWrite)
continue;
diff --git a/src/DomainCommonExtensions/CommonExtensions/SystemData/DataReaderExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/SystemData/DataReaderExtensions.cs
index f89aa78..c26b0b2 100644
--- a/src/DomainCommonExtensions/CommonExtensions/SystemData/DataReaderExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/SystemData/DataReaderExtensions.cs
@@ -122,6 +122,7 @@ public static bool ContainsColumnAndIsNotNullOrEmpty(this IDataReader dataReader
if (dataReader.GetOrdinal(columnName).IsGreaterThanOrEqualZero())
return dataReader[columnName] != DBNull.Value &&
!string.IsNullOrEmpty(dataReader[columnName].ToString());
+
return false;
}
catch (IndexOutOfRangeException)
diff --git a/src/DomainCommonExtensions/CommonExtensions/TypeExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/TypeExtensions.cs
index 99ce742..eed15fc 100644
--- a/src/DomainCommonExtensions/CommonExtensions/TypeExtensions.cs
+++ b/src/DomainCommonExtensions/CommonExtensions/TypeExtensions.cs
@@ -23,6 +23,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using DomainCommonExtensions.DataTypeExtensions;
+// ReSharper disable ExpressionIsAlwaysNull
#endregion
@@ -64,7 +65,7 @@ public static class TypeExtensions
///
public static object GetPropertyValue(this object obj, string name)
{
- if (obj == null)
+ if (obj.IsNull())
throw new ArgumentNullException(nameof(obj));
return obj?.GetType()
@@ -81,7 +82,7 @@ public static object GetPropertyValue(this object obj, string name)
///
public static string GetStringPropertyValue(this object obj, string name)
{
- if (obj == null)
+ if (obj.IsNull())
throw new ArgumentNullException(nameof(obj));
return obj?.GetType()
@@ -97,7 +98,7 @@ public static string GetStringPropertyValue(this object obj, string name)
///
public static bool IsAnonymousType(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false)
@@ -114,7 +115,7 @@ public static bool IsAnonymousType(this Type type)
///
public static bool IsNullablePropType(this Type type)
{
- if (type == null) throw new ArgumentNullException(nameof(type));
+ if (type.IsNull()) throw new ArgumentNullException(nameof(type));
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
@@ -133,7 +134,7 @@ public static T GetValue(this Type type)
var t = typeof(T);
t = Nullable.GetUnderlyingType(t) ?? t;
- return value == null || DBNull.Value.Equals(value)
+ return value.IsNull() || DBNull.Value.Equals(value)
? default
: (T)Convert.ChangeType(value, t);
}
@@ -147,21 +148,21 @@ public static T GetValue(this Type type)
///
public static string GetSelectedFieldFromEntity(this Type type, IEnumerable fields = null)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var searchBuilder = new StringBuilder();
var propsInfo = type.GetProperties();
var props = propsInfo.Select(x => x.Name).ToList();
- if (fields != null && fields.Any())
+ if (fields.IsNotNull() && fields.Any())
{
//Return only specified fields
foreach (var field in fields.ToList())
if (props.Contains(field))
{
var propType = propsInfo.FirstOrDefault(x => x.Name == field);
- if (propType != null) searchBuilder.Append($"{field},");
+ if (propType.IsNotNull()) searchBuilder.Append($"{field},");
}
}
else
@@ -181,7 +182,7 @@ public static string GetSelectedFieldFromEntity(this Type type, IEnumerable
public static bool IsSimpleType(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var underlyingType = Nullable.GetUnderlyingType(type);
@@ -219,7 +220,7 @@ public static bool IsSimpleType(this Type type)
///
public static bool IsTypeOfInt(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var underlyingType = Nullable.GetUnderlyingType(type);
@@ -245,7 +246,7 @@ public static bool IsTypeOfInt(this Type type)
///
public static bool IsTypeOfNullableInt(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var simpleTypes = new List
@@ -269,7 +270,7 @@ public static bool IsTypeOfNullableInt(this Type type)
///
public static bool IsTypeOfFloatingPoint(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var underlyingType = Nullable.GetUnderlyingType(type);
@@ -292,7 +293,7 @@ public static bool IsTypeOfFloatingPoint(this Type type)
///
public static bool IsTypeOfNullableFloatingPoint(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var simpleTypes = new List
@@ -313,7 +314,7 @@ public static bool IsTypeOfNullableFloatingPoint(this Type type)
///
public static bool IsEnumType(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var underlyingType = Nullable.GetUnderlyingType(type);
@@ -330,7 +331,7 @@ public static bool IsEnumType(this Type type)
///
public static bool IsStringType(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
var underlyingType = Nullable.GetUnderlyingType(type);
@@ -347,7 +348,7 @@ public static bool IsStringType(this Type type)
///
public static Type GetNonNullableType(this Type type)
{
- if (type == null)
+ if (type.IsNull())
throw new ArgumentNullException(nameof(type));
return type.IsNullablePropType()
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/BoolExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/BoolExtensions.cs
index 20f9bda..bdb0948 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/BoolExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/BoolExtensions.cs
@@ -14,6 +14,8 @@
//
// ***********************************************************************
+using DomainCommonExtensions.CommonExtensions;
+
namespace DomainCommonExtensions.DataTypeExtensions
{
///
@@ -23,9 +25,9 @@ namespace DomainCommonExtensions.DataTypeExtensions
public static class BoolExtensions
{
///
- /// Negate
+ /// Negate source value.
///
- ///
+ /// Source object to be checked.
///
public static bool Negate(this bool? source)
{
@@ -35,13 +37,47 @@ public static bool Negate(this bool? source)
}
///
- /// Negate
+ /// Negate source value.
///
- ///
+ /// Source object to be checked.
///
public static bool Negate(this bool source)
- {
- return !source;
- }
+ => !source;
+
+ ///
+ /// Check if source value is equals with true.
+ ///
+ /// Source object to be checked.
+ ///
+ ///
+ public static bool IsTrue(this bool source)
+ => source.IsNotNull() && source.Equals(true);
+
+ ///
+ /// Check if source value is equals with true.
+ ///
+ /// Source object to be checked.
+ ///
+ ///
+ public static bool IsTrue(this bool? source)
+ => source.IsNotNull() && source.Equals(true);
+
+ ///
+ /// Check if source value is equals with false.
+ ///
+ /// Source object to be checked.
+ ///
+ ///
+ public static bool IsFalse(this bool source)
+ => source.IsNull() || source.Equals(false);
+
+ ///
+ /// Check if source value is equals with false.
+ ///
+ /// Source object to be checked.
+ ///
+ ///
+ public static bool IsFalse(this bool? source)
+ => source.IsNull() || source.Equals(false);
}
}
\ No newline at end of file
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs
index 12f3cda..f25955b 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs
@@ -17,11 +17,14 @@
#region U S A G E S
using System;
+#if NET45_OR_GREATER || NET || NETSTANDARD1_0_OR_GREATER
using System.IO;
using System.IO.Compression;
+#endif
using System.Linq;
using System.Security.Cryptography;
using System.Text;
+using DomainCommonExtensions.CommonExtensions;
#endregion
@@ -41,7 +44,7 @@ public static class ByteExtensions
///
public static string ToBase64String(this byte[] bytes)
{
- if (bytes == null)
+ if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));
return Convert.ToBase64String(bytes, 0, bytes.Length);
@@ -55,7 +58,7 @@ public static string ToBase64String(this byte[] bytes)
///
public static string GetHashSha256String(this byte[] bytes)
{
- if (bytes == null)
+ if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));
var sb = new StringBuilder();
@@ -73,7 +76,7 @@ public static string GetHashSha256String(this byte[] bytes)
///
public static string GetHashSha1String(this byte[] bytes)
{
- if (bytes == null)
+ if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));
using var sha1 = new SHA1Managed();
@@ -92,7 +95,7 @@ public static string GetHashSha1String(this byte[] bytes)
// ReSharper disable once InconsistentNaming
public static string GetStringUTF8(this byte[] bytes)
{
- if (bytes == null)
+ if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));
return Encoding.UTF8.GetString(bytes);
@@ -105,7 +108,7 @@ public static string GetStringUTF8(this byte[] bytes)
/// The string
public static string GetString(this byte[] bytes)
{
- if (bytes == null)
+ if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));
return bytes.Aggregate(string.Empty, (current, b) => current + (char) b);
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs
index c20f406..541c4b7 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs
@@ -14,6 +14,8 @@
//
// ***********************************************************************
+using DomainCommonExtensions.CommonExtensions;
+
namespace DomainCommonExtensions.DataTypeExtensions
{
///
@@ -30,7 +32,7 @@ public static class DecimalExtensions
///
public static bool IsNullOrZero(this decimal? value)
{
- return value == null || value == 0;
+ return value.IsNull() || value == 0;
}
///
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/GuidExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/GuidExtensions.cs
index 552dafb..51ecdb2 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/GuidExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/GuidExtensions.cs
@@ -37,7 +37,7 @@ public static class GuidExtensions
///
public static bool IsGuid(this string source)
{
- if (string.IsNullOrEmpty(source)) return false;
+ if (source.IsNullOrEmpty()) return false;
var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
var guidRegEx = new Regex(RegularExpressions.GUID, options);
@@ -52,7 +52,7 @@ public static bool IsGuid(this string source)
///
public static Guid ToGuid(this string source)
{
- if (string.IsNullOrEmpty(source)) return Guid.Empty;
+ if (source.IsNullOrEmpty()) return Guid.Empty;
try
{
return Guid.Parse(source);
@@ -70,7 +70,7 @@ public static Guid ToGuid(this string source)
///
public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
{
- if (string.IsNullOrEmpty(source)) return Guid.Empty;
+ if (source.IsNullOrEmpty()) return Guid.Empty;
try
{
return Guid.ParseExact(source.Replace("-", "").Replace("\"", ""), "N");
@@ -88,7 +88,7 @@ public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
///
public static Guid? TryToGuid(this string source)
{
- if (string.IsNullOrEmpty(source)) return null;
+ if (source.IsNullOrEmpty()) return null;
try
{
return Guid.Parse(source);
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs
index 8e8c282..6c71ce7 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs
@@ -14,6 +14,8 @@
//
// ***********************************************************************
+using DomainCommonExtensions.CommonExtensions;
+
namespace DomainCommonExtensions.DataTypeExtensions
{
///
@@ -30,7 +32,7 @@ public static class IntExtensions
///
public static bool IsNullOrZero(this int? value)
{
- return value == null || value == 0;
+ return value.IsNull() || value == 0;
}
///
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs
index 0ea6fb2..09744d3 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs
@@ -14,6 +14,8 @@
//
// ***********************************************************************
+using DomainCommonExtensions.CommonExtensions;
+
namespace DomainCommonExtensions.DataTypeExtensions
{
///
@@ -30,7 +32,7 @@ public static class LongExtensions
///
public static bool IsNullOrZero(this long? value)
{
- return value == null || value == 0;
+ return value.IsNull() || value == 0;
}
///
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs
index 8e14226..c2af395 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs
@@ -45,7 +45,7 @@ public static class ObjectExtensions
///
public static DateTime? TryGetDateTimeFromDbObj(this object value)
{
- if (value == null || value == DBNull.Value || string.IsNullOrEmpty(value.ToString()))
+ if (value.IsNull() || value .IsDbNull() || string.IsNullOrEmpty(value.ToString()))
return null;
try
{
@@ -120,7 +120,7 @@ public static void CopyPropertiesTo(this object fromObject, object toObject)
public static Hashtable GetPropertyHash(this object properties)
{
Hashtable values = null;
- if (properties != null)
+ if (properties.IsNotNull())
{
values = new Hashtable();
var props = TypeDescriptor.GetProperties(properties);
@@ -166,7 +166,7 @@ public static IDictionary ToDictionary(this object obj)
}
///
- /// throw exception if source is null
+ /// Throw exception if source is null
///
/// Source to check
/// Error message
@@ -177,6 +177,30 @@ public static void ThrowIfNull(this object source, string msg)
throw new Exception(msg);
}
+ ///
+ /// Throw argument null exception if source is null
+ ///
+ /// Source to check
+ /// Name of source object
+ ///
+ public static void ThrowIfArgNull(this object source, string objectName)
+ {
+ if (source.IsNull())
+ throw new ArgumentNullException(objectName);
+ }
+
+ ///
+ /// Throw argument exception if source is null
+ ///
+ /// Source to check
+ /// Error message.
+ ///
+ public static void ThrowArgIfNull(this object source, string msg)
+ {
+ if (source.IsNull())
+ throw new ArgumentException(msg);
+ }
+
///
/// Cast object to string
///
@@ -185,7 +209,7 @@ public static void ThrowIfNull(this object source, string msg)
///
public static string ToString(this object source)
{
- if (source == System.DBNull.Value || source.IsNull())
+ if (source.IsDbNull() || source.IsNull())
return null;
else
return source.ToString();
@@ -200,7 +224,7 @@ public static string ToString(this object source)
///
public static T To(this object source)
{
- if (source == System.DBNull.Value || source.IsNull())
+ if (source.IsDbNull() || source.IsNull())
return default;
else
return (T)source;
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
index 1cffa0c..02402c5 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
@@ -69,7 +69,7 @@ public static string[] Split(this string str, string delimiter)
///
public static string FirstCharToUpper(this string input)
{
- if (string.IsNullOrEmpty(input) || input.Length < 2) return input;
+ if (input.IsNullOrEmpty() || input.Length < 2) return input;
return input.First().ToString().ToUpper() + input.Substring(1);
}
@@ -81,7 +81,7 @@ public static string FirstCharToUpper(this string input)
///
public static string FirstCharToLower(this string input)
{
- if (string.IsNullOrEmpty(input) || input.Length < 2) return input;
+ if (input.IsNullOrEmpty() || input.Length < 2) return input;
return input.First().ToString().ToLowerInvariant() + input.Substring(1);
}
@@ -173,7 +173,7 @@ public static string Truncate(this string text, int maxLength, bool useDots = fa
///
public static string TruncateExactLength(this string value, int maxLength)
{
- if (string.IsNullOrEmpty(value)) return value;
+ if (value.IsNullOrEmpty()) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
@@ -463,7 +463,7 @@ public static string MoveUpInDirectoryBackAppend(this string currentPath, int no
///
public static string RemoveSpecialChars(this string str)
{
- if (string.IsNullOrEmpty(str)) return str;
+ if (str.IsNullOrEmpty()) return str;
var chars = new[]
{
@@ -608,7 +608,7 @@ public static byte[] DeCodeBytesFromBase64(this string encoded)
///
public static bool IsBase64String(this string base64String)
{
- if (string.IsNullOrEmpty(base64String)) return false;
+ if (base64String.IsNullOrEmpty()) return false;
base64String = base64String.TrimIfNotNull();
@@ -624,7 +624,7 @@ public static bool IsBase64String(this string base64String)
///
public static string ReplaceSpecialCharacters(this string data)
{
- if (data is null) return null;
+ if (data.IsNull()) return null;
var normalizedString = data.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
@@ -661,6 +661,7 @@ public static string Next(this string value)
var alternative = new StringBuilder();
for (var i = 0; i <= value.Length; i++) alternative.Append('A');
+
return alternative.ToString();
}
@@ -692,7 +693,7 @@ public static string Next(this string value, uint next)
///
public static string FromUnicode(this string unicodeString)
{
- if (string.IsNullOrEmpty(unicodeString)) return unicodeString;
+ if (unicodeString.IsNullOrEmpty()) return unicodeString;
var bytes = Encoding.Unicode.GetBytes(unicodeString);
var encoded = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes);
@@ -708,10 +709,11 @@ public static string FromUnicode(this string unicodeString)
///
public static Guid? TryParseGuidNullable(this string val)
{
- if (string.IsNullOrEmpty(val))
+ if (val.IsNullOrEmpty())
return null;
if (Guid.TryParse(val, out var guidValue))
return guidValue;
+
return null;
}
@@ -934,7 +936,7 @@ public static string FixBase64ForImage(this string image)
///
public static T ToEnum(this string value)
{
- if (value == null)
+ if (value.IsNull())
throw new ArgumentNullException(nameof(value));
try
@@ -1002,7 +1004,7 @@ public static string IfNullOrEmpty(this string input, string defaultValue)
///
public static string[] ToLines(this string source)
{
- if (source == null) return new string[0];
+ if (source.IsNull()) return new string[0];
var temp = source;
temp = temp.Replace("\r\n", "\n");
@@ -1047,7 +1049,7 @@ public static IEnumerable Chunked(this string source, int size, bool isR
}
///
- /// throw exception if source is null or empty
+ /// Throw exception if source is null or empty
///
/// Source to check
/// Error message
@@ -1059,7 +1061,7 @@ public static void ThrowIfNullOrEmpty(this string source, string msg)
}
///
- /// throw exception if source is null
+ /// Throw exception if source is null
///
/// Source to check
/// Error message
@@ -1071,7 +1073,55 @@ public static void ThrowIfNull(this string source, string msg)
}
///
- /// throw exception if source is empty
+ /// Throw argument exception if source is null
+ ///
+ /// Source to check
+ /// Error message.
+ ///
+ public static void ThrowArgIfNull(this string source, string msg)
+ {
+ if (source.IsNull())
+ throw new ArgumentException(msg);
+ }
+
+ ///
+ /// Throw argument exception if source is null or empty
+ ///
+ /// Source to check
+ /// Error message.
+ ///
+ public static void ThrowArgIfNullOrEmpty(this string source, string msg)
+ {
+ if (source.IsNullOrEmpty())
+ throw new ArgumentException(msg);
+ }
+
+ ///
+ /// Throw argument null exception if source is null
+ ///
+ /// Source to check
+ /// Object name
+ ///
+ public static void ThrowIfArgNull(this string source, string objectName)
+ {
+ if (source.IsNull())
+ throw new ArgumentNullException(objectName);
+ }
+
+ ///
+ /// Throw argument null exception if source is null or empty
+ ///
+ /// Source to check
+ /// Object name
+ ///
+ public static void ThrowIfArgNullOrEmpty(this string source, string objectName)
+ {
+ if (source.IsNullOrEmpty())
+ throw new ArgumentNullException(objectName);
+ }
+
+ ///
+ /// Throw exception if source is empty
///
/// Source to check
/// Error message
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/StringInjectExtension.cs b/src/DomainCommonExtensions/DataTypeExtensions/StringInjectExtension.cs
index 2eb3289..36b4595 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/StringInjectExtension.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/StringInjectExtension.cs
@@ -19,6 +19,7 @@
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;
+using DomainCommonExtensions.CommonExtensions;
#endregion
@@ -64,7 +65,7 @@ public static string Inject(this string formatString, IDictionary dictionary)
public static string Inject(this string formatString, Hashtable attributes)
{
var result = formatString;
- if (attributes == null || formatString == null)
+ if (attributes.IsNull() || formatString.IsNull())
return result;
foreach (string attributeKey in attributes.Keys)
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/ULongExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/ULongExtensions.cs
index 1015216..2cfa859 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/ULongExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/ULongExtensions.cs
@@ -14,6 +14,8 @@
//
// ***********************************************************************
+using DomainCommonExtensions.CommonExtensions;
+
namespace DomainCommonExtensions.DataTypeExtensions
{
///
@@ -30,7 +32,7 @@ public static class ULongExtensions
///
public static bool IsNullOrZero(this ulong? value)
{
- return value == null || value == 0;
+ return value.IsNull() || value == 0;
}
///
diff --git a/src/shared/GeneralAssemblyInfo.cs b/src/shared/GeneralAssemblyInfo.cs
index bdbf9bf..dcdc23e 100644
--- a/src/shared/GeneralAssemblyInfo.cs
+++ b/src/shared/GeneralAssemblyInfo.cs
@@ -47,6 +47,6 @@
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
#endif
-[assembly: AssemblyVersion("1.0.8.1533")]
-[assembly: AssemblyFileVersion("1.0.8.1533")]
-[assembly: AssemblyInformationalVersion("1.0.8.x")]
\ No newline at end of file
+[assembly: AssemblyVersion("1.0.9.2108")]
+[assembly: AssemblyFileVersion("1.0.9.2108")]
+[assembly: AssemblyInformationalVersion("1.0.9.x")]
\ No newline at end of file
diff --git a/src/tests/DataTypeTests/ObjectTests.cs b/src/tests/DataTypeTests/ObjectTests.cs
index 21711c8..10a1dd3 100644
--- a/src/tests/DataTypeTests/ObjectTests.cs
+++ b/src/tests/DataTypeTests/ObjectTests.cs
@@ -50,5 +50,21 @@ public void TryCastTest()
Assert.IsTrue(castString);
Assert.IsFalse(castNull);
}
+
+ [TestMethod]
+ public void ThrowIfNullTest()
+ {
+ object obj = null;
+
+ Assert.ThrowsException(() => obj.ThrowIfNull("Obj is null"), "Obj is null");
+ }
+
+ [TestMethod]
+ public void ThrowIfArgNullTest()
+ {
+ object obj = null;
+
+ Assert.ThrowsException(() => obj.ThrowIfArgNull(nameof(obj)), "Value cannot be null. (Parameter 'obj')");
+ }
}
}
\ No newline at end of file