Skip to content

Commit

Permalink
Merge pull request #15 from I-RzR-I/feature/09-2023-AddNewExtensionMe…
Browse files Browse the repository at this point in the history
…thods

Add extensions, adjust validation, refactor.
  • Loading branch information
I-RzR-I authored Sep 27, 2023
2 parents 517631f + 6ddff41 commit 973188b
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 87 deletions.
10 changes: 9 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@

### **v.1.0.8.0638**
-> Added string extension `GetHashSha512String`, `FromSpaceSeparatedString`, `IsMissing`, `IsNullOrEmpty`, `AddQueryString`, `AddHashFragment`, `GetOrigin`, `Obfuscate`.<br />
-> Added Enumerable extension `ToSpaceSeparatedString`, `HasDuplicates`, `GetDuplicates`.<br />
-> Added Enumerable extension `ToSpaceSeparatedString`, `HasDuplicates`, `GetDuplicates`.<br />

### **v.1.0.9.2108**
-> Added object extensions `ThrowIfArgNull`, `ThrowArgIfNull`.<br />
-> Added bool extensions `IsTrue`, `IsFalse`.<br />
-> Added null check extensions `IsNotNull`, `IsDbNull`.<br />
-> Added string extensions `ThrowArgIfNull`, `ThrowArgIfNullOrEmpty`, `ThrowIfArgNull`, `ThrowIfArgNullOrEmpty`.<br />
-> Adjust validation for input params at some methods.
-> Small code refactor.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static IEnumerable<T> Replace<T>(this IEnumerable<T> enumerable, int inde
/// <returns></returns>
public static string Join(this IEnumerable<string> source, string separator)
{
return source == null ? string.Empty : string.Join(separator, source);
return source.IsNull() ? string.Empty : string.Join(separator, source);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static XmlElement GetDocumentation(this MethodInfo methodInfo)
/// <returns>The XML fragment describing the member</returns>
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);
Expand All @@ -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();
}

/// <summary>
Expand All @@ -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();
}

/// <summary>
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using DomainCommonExtensions.DataTypeExtensions;

#endregion

Expand All @@ -37,6 +38,9 @@ public static partial class CryptoExtensions
/// <returns></returns>
public static string AesEncryptString(this string plainText, string key)
{
key.ThrowIfArgNullOrEmpty(nameof(key));
plainText.ThrowIfArgNull(nameof(plainText));

var iv = new byte[16];
byte[] array;

Expand Down Expand Up @@ -72,6 +76,9 @@ public static string AesEncryptString(this string plainText, string key)
/// <returns></returns>
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using DomainCommonExtensions.DataTypeExtensions;

// ReSharper disable CheckNamespace

namespace DomainCommonExtensions.CommonExtensions
Expand All @@ -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 };
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#region U S A G E S

using System;
using System.Collections.Generic;

#endregion
Expand All @@ -31,12 +32,10 @@ public static class NullExtensions
/// <summary>
/// Is null
/// </summary>
/// <param name="obj"></param>
/// <param name="obj">Object to be checked</param>
/// <returns></returns>
public static bool IsNull(this object obj)
{
return obj == null;
}
=> obj == null;

/// <summary>
/// Check if KeyValue is null
Expand All @@ -49,5 +48,21 @@ public static bool IsNull<TK, TV>(this KeyValuePair<TK, TV> source)
{
return source.Equals(default(KeyValuePair<TK, TV>));
}

/// <summary>
/// Is not null
/// </summary>
/// <param name="obj">Object to be checked</param>
/// <returns></returns>
public static bool IsNotNull(this object obj)
=> obj != null;

/// <summary>
/// Is if source object is DBNull
/// </summary>
/// <param name="obj">Object to be checked</param>
/// <returns></returns>
public static bool IsDbNull(this object obj)
=> obj == DBNull.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> exp
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters);

return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}
Expand All @@ -79,6 +80,7 @@ public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> ex
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters);

return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 17 additions & 16 deletions src/DomainCommonExtensions/CommonExtensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using DomainCommonExtensions.DataTypeExtensions;
// ReSharper disable ExpressionIsAlwaysNull

#endregion

Expand Down Expand Up @@ -64,7 +65,7 @@ public static class TypeExtensions
/// <remarks></remarks>
public static object GetPropertyValue(this object obj, string name)
{
if (obj == null)
if (obj.IsNull())
throw new ArgumentNullException(nameof(obj));

return obj?.GetType()
Expand All @@ -81,7 +82,7 @@ public static object GetPropertyValue(this object obj, string name)
/// <remarks></remarks>
public static string GetStringPropertyValue(this object obj, string name)
{
if (obj == null)
if (obj.IsNull())
throw new ArgumentNullException(nameof(obj));

return obj?.GetType()
Expand All @@ -97,7 +98,7 @@ public static string GetStringPropertyValue(this object obj, string name)
/// <remarks></remarks>
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)
Expand All @@ -114,7 +115,7 @@ public static bool IsAnonymousType(this Type type)
/// <remarks></remarks>
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<>);
}
Expand All @@ -133,7 +134,7 @@ public static T GetValue<T>(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);
}
Expand All @@ -147,21 +148,21 @@ public static T GetValue<T>(this Type type)
/// <remarks></remarks>
public static string GetSelectedFieldFromEntity(this Type type, IEnumerable<string> 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
Expand All @@ -181,7 +182,7 @@ public static string GetSelectedFieldFromEntity(this Type type, IEnumerable<stri
/// <remarks></remarks>
public static bool IsSimpleType(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var underlyingType = Nullable.GetUnderlyingType(type);
Expand Down Expand Up @@ -219,7 +220,7 @@ public static bool IsSimpleType(this Type type)
/// <remarks></remarks>
public static bool IsTypeOfInt(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var underlyingType = Nullable.GetUnderlyingType(type);
Expand All @@ -245,7 +246,7 @@ public static bool IsTypeOfInt(this Type type)
/// <remarks></remarks>
public static bool IsTypeOfNullableInt(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var simpleTypes = new List<Type>
Expand All @@ -269,7 +270,7 @@ public static bool IsTypeOfNullableInt(this Type type)
/// <remarks></remarks>
public static bool IsTypeOfFloatingPoint(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var underlyingType = Nullable.GetUnderlyingType(type);
Expand All @@ -292,7 +293,7 @@ public static bool IsTypeOfFloatingPoint(this Type type)
/// <remarks></remarks>
public static bool IsTypeOfNullableFloatingPoint(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var simpleTypes = new List<Type>
Expand All @@ -313,7 +314,7 @@ public static bool IsTypeOfNullableFloatingPoint(this Type type)
/// <remarks></remarks>
public static bool IsEnumType(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var underlyingType = Nullable.GetUnderlyingType(type);
Expand All @@ -330,7 +331,7 @@ public static bool IsEnumType(this Type type)
/// <remarks></remarks>
public static bool IsStringType(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

var underlyingType = Nullable.GetUnderlyingType(type);
Expand All @@ -347,7 +348,7 @@ public static bool IsStringType(this Type type)
/// <remarks></remarks>
public static Type GetNonNullableType(this Type type)
{
if (type == null)
if (type.IsNull())
throw new ArgumentNullException(nameof(type));

return type.IsNullablePropType()
Expand Down
Loading

0 comments on commit 973188b

Please sign in to comment.