diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-12-10 00:39:06 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-12-10 00:39:06 +0200 |
| commit | 3f069bb4a5303b2c732ba1263229f62526acc693 (patch) | |
| tree | 9426ff213bf43e8ed80f3c5265518a98d33cc63f /Software/Visual_Studio/Tango.Core/ExtensionMethods | |
| parent | d5827e26ff5ee1b0532530bce4da3533f71a63dd (diff) | |
| download | Tango-3f069bb4a5303b2c732ba1263229f62526acc693.tar.gz Tango-3f069bb4a5303b2c732ba1263229f62526acc693.zip | |
Refactored Object mapping extension methods !
Removed FK from JOB_RUNS => JOBS
Added MACHINE_GUID to JOB_RUNS
Refactored MS Statistics module for the new changes.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs | 113 |
1 files changed, 59 insertions, 54 deletions
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs index fe2c7ff9a..e8ef9addd 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs @@ -12,6 +12,34 @@ using Tango.Serialization; namespace Tango.Core.ExtensionMethods { + public enum MappingFlags + { + /// <summary> + /// All properties will be mapped. + /// </summary> + All = 1, + + /// <summary> + /// Do not map string properties. + /// </summary> + NoStrings = 2, + + /// <summary> + /// Do not map string properties with value of null. + /// </summary> + NoNullStrings = 4, + + /// <summary> + /// Do not map reference types. + /// </summary> + NoReferenceTypes = 8, + + /// <summary> + /// Map only primitive values. + /// </summary> + PrimitivesOnly, + } + /// <summary> /// Contains <see cref="Object"/> extension methods. /// </summary> @@ -58,85 +86,62 @@ namespace Tango.Core.ExtensionMethods } /// <summary> - /// Maps the object properties values to the destination object. + /// Maps the object primitive properties values to the destination object. /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - public static void MapPrimitivesTo(this object source, object destination) + /// <param name="source">The source object.</param> + /// <param name="destination">The destination object.</param> + /// <param name="flags">The mapping flags.</param> + /// <param name="condition">Optional condition.</param> + public static void MapPropertiesTo(this object source, object destination, MappingFlags flags = MappingFlags.NoReferenceTypes, Func<PropertyInfo, bool> condition = null) { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) + foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); + var propType = prop.PropertyType; + var value = prop.GetValue(source); - if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) + if (condition != null) { - desProp.SetValue(destination, prop.GetValue(source)); + if (!condition(prop)) continue; } - } - } - /// <summary> - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - public static void MapPrimitivesWithStringsNoNullsTo(this object source, object destination) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) - { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); + if (!propType.IsPrimitive && flags.HasFlag(MappingFlags.PrimitivesOnly)) + { + continue; + } - if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + if (propType == typeof(String) && flags.HasFlag(MappingFlags.NoStrings)) { - var value = prop.GetValue(source); + continue; + } - if (desProp.PropertyType != typeof(String) || !String.IsNullOrEmpty(value as String)) - { - desProp.SetValue(destination, value); - } + if (propType.IsClass && propType != typeof(String) && flags.HasFlag(MappingFlags.NoReferenceTypes)) + { + continue; + } + + if (flags.HasFlag(MappingFlags.NoNullStrings) && propType == typeof(String) && String.IsNullOrEmpty(value as String)) + { + continue; } - } - } - /// <summary> - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - public static void MapPrimitivesWithStrings(this object source, object destination) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) - { var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + if (desProp != null && desProp.PropertyType == prop.PropertyType && desProp.SetMethod != null) { - var value = prop.GetValue(source); - desProp.SetValue(destination, value); } } } + /// <summary> - /// Maps the object properties values to the destination object. + /// Maps the object primitive properties values to the destination object. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> - public static void MapPrimitivesTo(this object source, object destination, Func<PropertyInfo, bool> condition) + public static void MapPrimitivesTo(this object source, object destination) { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) - { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - - if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) - { - if (condition(prop)) - { - desProp.SetValue(destination, prop.GetValue(source)); - } - } - } + source.MapPropertiesTo(destination, MappingFlags.PrimitivesOnly); } /// <summary> |
