Files
xCommons/Extensions/ModelExtensions.cs
T
2024-01-25 04:39:39 +03:30

283 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
namespace xCommons.Extensions {
public static partial class ModelExtensions {
/// <summary>
/// Update an Instance of a Typed Object with Data Provided by another Instance of Typed Object
/// </summary>
/// <param name="source"></param>
/// <param name="updateWith"></param>
/// <param name="propertyWhiteList"></param>
/// <param name="propertyBlackList"></param>
/// <param name="propertyValueProviders"></param>
/// <param name="updateWithNullOrEmptyValues"></param>
/// <param name="throwExceptionOnFails"></param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
/// <returns></returns>
public static T UpdateData<T, D> (
this T source,
D updateWith,
ICollection<string> propertyWhiteList = null,
ICollection<string> propertyBlackList = null,
ICollection<KeyValuePair<string, Func<D, object>>> propertyValueProviders = null,
bool updateWithNullOrEmptyValues = false,
bool throwExceptionOnFails = false
)
where T : class
where D : class {
//
var hasWhiteList = propertyWhiteList.HasChild<string> ();
var hasBlackList = propertyBlackList.HasChild<string> ();
var hasValueProvider = propertyValueProviders.HasChild<KeyValuePair<string, Func<D, object>>> ();
//
var fullPropertyList = updateWith.GetType ().GetProperties ().Select (prop => prop.Name);
var mustSetProps = (hasWhiteList ? propertyWhiteList : fullPropertyList);
//
if (hasBlackList) {
mustSetProps = mustSetProps.Where (propName => !propertyBlackList.Contains (propName));
}
//
mustSetProps.ToList ().ForEach (propName => {
//
try {
//
var sourceProp = source.GetType ().GetProperty (propName);
var sourceValue = sourceProp.GetValue (source);
var sourceValueType = sourceValue.IsNull () ? null : sourceValue.GetType ();
//
var updateWithProp = updateWith.GetType ().GetProperty (propName);
var updateWithValue = updateWithProp.GetValue (updateWith);
var updateWithValueType = updateWithValue.IsNull () ? null : updateWithValue.GetType ();
//
var valueProviderValue = hasValueProvider ? propertyValueProviders
.FirstOrDefault (p => p.Key == propName).Value : null;
var updateVal = !valueProviderValue.IsNull () ?
valueProviderValue.Invoke (updateWith) :
(updateWithValue.IsNull () && !updateWithNullOrEmptyValues) ?
sourceValue : updateWithValue;
var updateValType = updateVal.IsNull () ? null : updateVal.GetType ();
//
Type t = Nullable.GetUnderlyingType (sourceProp.PropertyType) ?? sourceProp.PropertyType;
object safeValue =
(updateVal == null) ?
null :
!updateValType.IsCollectionType () ?
Convert.ChangeType (updateVal, t) :
updateVal;
//
sourceProp.SetValue (source, safeValue);
} catch (Exception ex) {
//
if (throwExceptionOnFails) {
throw ex;
}
}
});
//
return source;
}
/// <summary>
/// Check two Instance of Typed Object Values are Same or not
/// </summary>
/// <param name="source"></param>
/// <param name="dest"></param>
/// <param name="propertyWhiteList"></param>
/// <param name="propertyBlackList"></param>
/// <param name="throwExceptionOnFails"></param>
/// <param name="propertyValueCheckers"></param>
/// /// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
/// <returns></returns>
public static bool IsSameContent<T, D> (
this T source,
D dest,
ICollection<string> propertyWhiteList = null,
ICollection<string> propertyBlackList = null,
ICollection<KeyValuePair<string, Func<T, D, bool>>> propertyValueCheckers = null,
bool throwExceptionOnFails = false
)
where T : class
where D : class {
//
var hasWhiteList = propertyWhiteList.HasChild<string> ();
var hasBlackList = propertyBlackList.HasChild<string> ();
var hasCheckerProvider = propertyValueCheckers.HasChild<KeyValuePair<string, Func<T, D, bool>>> ();
//
var fullPropertyList = dest.GetType ().GetProperties ().Select (prop => prop.Name);
var mustCheckProps = (hasWhiteList ? propertyWhiteList : fullPropertyList);
if (hasBlackList) {
mustCheckProps = mustCheckProps.Where (propName => !propertyBlackList.Contains (propName));
}
//
var result = true;
mustCheckProps.ToList ().ForEach (propName => {
//
try {
//
var sourceProp = source.GetType ().GetProperty (propName);
var destProp = dest.GetType ().GetProperty (propName);
//
// Check Types ...
var isSameType = true;
//
// Check
var isSameValue = false;
var mustDoCustomCheck = hasCheckerProvider && propertyValueCheckers.Any (p => p.Key == propName);
if (mustDoCustomCheck) {
//
// Check Values ...
var valueChecker = propertyValueCheckers.FirstOrDefault (p => p.Key == propName).Value;
isSameValue = valueChecker.Invoke (source, dest);
} else {
// //
// isSameType = (sourceProp.PropertyType == destProp.PropertyType &&
// sourceProp.ReflectedType == destProp.ReflectedType);
//
// Check Values ...
isSameValue = Equals (sourceProp.GetValue (source), destProp.GetValue (dest));
}
//
if (!isSameType || !isSameValue) {
result = false;
}
} catch (Exception ex) {
//
if (throwExceptionOnFails) {
throw ex;
}
}
});
//
return result;
}
/// <summary>
/// Check specific type is Collection Type or not
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsCollectionType (this Type source) {
//
if (source.IsNull () || !source.IsGenericType) {
return false;
}
//
var genericTypeDefinition = source.GetGenericTypeDefinition ();
var result = genericTypeDefinition == typeof (List<>) ||
genericTypeDefinition == typeof (HashSet<>) ||
genericTypeDefinition == typeof (Collection<>) ||
genericTypeDefinition == typeof (ICollection<>) ||
genericTypeDefinition == typeof (IEnumerable<>);
//
return result;
}
/// <summary>
/// Retrieve a Property Value in Generic
/// </summary>
/// <param name="item"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static string GetPropValues<T> (this T item)
where T : class {
//
var props = item.GetType ().GetProperties ();
var vals = props.Select (p => p.GetValue (item));
//
return vals.ToJSON ();
}
/// <summary>
/// Check a Property Value Contains specific value
/// </summary>
/// <param name="item"></param>
/// <param name="value"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static bool PropValuesContains<T> (this T item, string value)
where T : class {
//
return item.GetPropValues ()
.ToNormalString ()
.Contains (value
.ToNormalString ());
}
/// <summary>
/// Generate Default Column Map
/// </summary>
/// <param name="item"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IDictionary<string, Expression<Func<T, object>>> GetDefaultColumnsMap<T> (this T item)
where T : class {
//
if (item.IsNull ()) {
return null;
}
//
var result = new Dictionary<string, Expression<Func<T, object>>> ();
var props = item.GetType ().GetProperties ();
//
foreach (var prop in props) {
result.Add (prop.Name, t => t.GetType ().GetProperty (prop.Name).GetValue (t, null));
}
//
return result;
}
/// <summary>
/// Generate Default Column Map
/// </summary>
/// <param name="item"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IDictionary<string, string> GetPropValuesDictionary<T> (this T item)
where T : class {
//
if (item.IsNull ()) {
return null;
}
//
var result = new Dictionary<string, string> ();
var props = item.GetType ().GetProperties ();
//
foreach (var prop in props) {
result.Add (prop.Name, prop.GetValue (item).ToJSON ());
}
//
return result;
}
}
}