From dae78d21de987cd9992c5c4fbf11dae1d302f09e Mon Sep 17 00:00:00 2001 From: "grzegorz.russek" Date: Fri, 28 Sep 2018 17:38:06 +0000 Subject: [PATCH] --- AmalgamationTool/DynamORM.Amalgamation.cs | 112 +++++++++++++++++++++- DynamORM/DynamicExtensions.cs | 75 +++++++++++++++ DynamORM/Mapper/DynamicTypeMap.cs | 31 ++++++ DynamORM/Validation/RequiredAttribute.cs | 4 +- DynamORM/Validation/ValidationResult.cs | 3 + 5 files changed, 221 insertions(+), 4 deletions(-) diff --git a/AmalgamationTool/DynamORM.Amalgamation.cs b/AmalgamationTool/DynamORM.Amalgamation.cs index dbf2055..26181c6 100644 --- a/AmalgamationTool/DynamORM.Amalgamation.cs +++ b/AmalgamationTool/DynamORM.Amalgamation.cs @@ -4963,6 +4963,35 @@ namespace DynamORM return (T)mapper.Create(item); } + /// MapEnumerable object enumerator into specified type using property names. + /// Type to which columnMap results. + /// Source enumerator. + /// Enumerator of specified type. + public static IEnumerable MapEnumerableByProperty(this IEnumerable enumerable) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + foreach (object item in enumerable) + yield return (T)mapper.CreateByProperty(item); + } + + /// MapEnumerable object item into specified type using property names. + /// Type to which columnMap results. + /// Source object. + /// Item of specified type. + public static T MapByProperty(this object item) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + return (T)mapper.CreateByProperty(item); + } + /// Fill object of specified type with data from source object. /// Type to which columnMap results. /// Item to which columnMap data. @@ -4980,6 +5009,23 @@ namespace DynamORM return item; } + /// Fill object of specified type with data from source object using property names. + /// Type to which columnMap results. + /// Item to which columnMap data. + /// Item from which extract data. + /// Filled item. + public static T FillByProperty(this T item, object source) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + mapper.MapByProperty(item, source); + + return item; + } + /// MapEnumerable object enumerator into specified type. /// Source enumerator. /// Type to which columnMap results. @@ -5009,6 +5055,35 @@ namespace DynamORM return mapper.Create(item); } + /// MapEnumerable object enumerator into specified type using property names. + /// Source enumerator. + /// Type to which columnMap results. + /// Enumerator of specified type. + public static IEnumerable MapEnumerableByProperty(this IEnumerable enumerable, Type type) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(type); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + foreach (object item in enumerable) + yield return mapper.CreateByProperty(item); + } + + /// MapEnumerable object item into specified type using property names. + /// Source object. + /// Type to which columnMap results. + /// Item of specified type. + public static object MapByProperty(this object item, Type type) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(type); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + return mapper.CreateByProperty(item); + } + /// Converts the elements of an /// to the specified type. /// The type to convert the elements of source to. @@ -13397,6 +13472,14 @@ namespace DynamORM return Map(source, Creator()); } + /// Create object of type and fill values from source using property names. + /// Object containing values that will be mapped to newly created object. + /// New object of type with matching values from source. + public object CreateByProperty(object source) + { + return MapByProperty(source, Creator()); + } + /// Fill values from source to object in destination. /// Object containing values that will be mapped to newly created object. /// Object of type to which copy values from source. @@ -13415,6 +13498,26 @@ namespace DynamORM return destination; } + /// Fill values from source to object in destination using property names. + /// Object containing values that will be mapped to newly created object. + /// Object of type to which copy values from source. + /// Object of type with matching values from source. + public object MapByProperty(object source, object destination) + { + string cn = null; + DynamicPropertyInvoker dpi = null; + + foreach (KeyValuePair item in source.ToDictionary()) + { + if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) + if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) + if (dpi.Setter != null) + dpi.Set(destination, item.Value); + } + + return destination; + } + /// Validates the object. /// The value. /// List of not valid results. @@ -13471,6 +13574,7 @@ namespace DynamORM Property = prop, Requirement = r, Value = item, + Result = validelem, }); } } @@ -13493,6 +13597,7 @@ namespace DynamORM Property = prop, Requirement = r, Value = v, + Result = valid, }); } } @@ -13690,12 +13795,12 @@ namespace DynamORM { int? cnt = null; - var list = (val as IEnumerable); + var list = val as IEnumerable; if (list != null) cnt = list.Count(); else { - var enumerable = (val as IEnumerable); + var enumerable = val as IEnumerable; if (enumerable != null) cnt = enumerable.Cast().Count(); } @@ -13773,6 +13878,9 @@ namespace DynamORM /// Gets the value that is broken. public object Value { get; internal set; } + + /// Gets the result. + public ValidateResult Result { get; internal set; } } } } \ No newline at end of file diff --git a/DynamORM/DynamicExtensions.cs b/DynamORM/DynamicExtensions.cs index a56cac1..a8d956b 100644 --- a/DynamORM/DynamicExtensions.cs +++ b/DynamORM/DynamicExtensions.cs @@ -1499,6 +1499,35 @@ namespace DynamORM return (T)mapper.Create(item); } + /// MapEnumerable object enumerator into specified type using property names. + /// Type to which columnMap results. + /// Source enumerator. + /// Enumerator of specified type. + public static IEnumerable MapEnumerableByProperty(this IEnumerable enumerable) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + foreach (object item in enumerable) + yield return (T)mapper.CreateByProperty(item); + } + + /// MapEnumerable object item into specified type using property names. + /// Type to which columnMap results. + /// Source object. + /// Item of specified type. + public static T MapByProperty(this object item) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + return (T)mapper.CreateByProperty(item); + } + /// Fill object of specified type with data from source object. /// Type to which columnMap results. /// Item to which columnMap data. @@ -1516,6 +1545,23 @@ namespace DynamORM return item; } + /// Fill object of specified type with data from source object using property names. + /// Type to which columnMap results. + /// Item to which columnMap data. + /// Item from which extract data. + /// Filled item. + public static T FillByProperty(this T item, object source) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + mapper.MapByProperty(item, source); + + return item; + } + /// MapEnumerable object enumerator into specified type. /// Source enumerator. /// Type to which columnMap results. @@ -1545,6 +1591,35 @@ namespace DynamORM return mapper.Create(item); } + /// MapEnumerable object enumerator into specified type using property names. + /// Source enumerator. + /// Type to which columnMap results. + /// Enumerator of specified type. + public static IEnumerable MapEnumerableByProperty(this IEnumerable enumerable, Type type) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(type); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + foreach (object item in enumerable) + yield return mapper.CreateByProperty(item); + } + + /// MapEnumerable object item into specified type using property names. + /// Source object. + /// Type to which columnMap results. + /// Item of specified type. + public static object MapByProperty(this object item, Type type) + { + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(type); + + if (mapper == null) + throw new InvalidOperationException("Type can't be mapped for unknown reason."); + + return mapper.CreateByProperty(item); + } + /// Converts the elements of an /// to the specified type. /// The type to convert the elements of source to. diff --git a/DynamORM/Mapper/DynamicTypeMap.cs b/DynamORM/Mapper/DynamicTypeMap.cs index 751de68..95eebb2 100644 --- a/DynamORM/Mapper/DynamicTypeMap.cs +++ b/DynamORM/Mapper/DynamicTypeMap.cs @@ -122,6 +122,15 @@ namespace DynamORM.Mapper return Map(source, Creator()); } + + /// Create object of type and fill values from source using property names. + /// Object containing values that will be mapped to newly created object. + /// New object of type with matching values from source. + public object CreateByProperty(object source) + { + return MapByProperty(source, Creator()); + } + /// Fill values from source to object in destination. /// Object containing values that will be mapped to newly created object. /// Object of type to which copy values from source. @@ -140,6 +149,26 @@ namespace DynamORM.Mapper return destination; } + /// Fill values from source to object in destination using property names. + /// Object containing values that will be mapped to newly created object. + /// Object of type to which copy values from source. + /// Object of type with matching values from source. + public object MapByProperty(object source, object destination) + { + string cn = null; + DynamicPropertyInvoker dpi = null; + + foreach (KeyValuePair item in source.ToDictionary()) + { + if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) + if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) + if (dpi.Setter != null) + dpi.Set(destination, item.Value); + } + + return destination; + } + /// Validates the object. /// The value. /// List of not valid results. @@ -196,6 +225,7 @@ namespace DynamORM.Mapper Property = prop, Requirement = r, Value = item, + Result = validelem, }); } } @@ -218,6 +248,7 @@ namespace DynamORM.Mapper Property = prop, Requirement = r, Value = v, + Result = valid, }); } } diff --git a/DynamORM/Validation/RequiredAttribute.cs b/DynamORM/Validation/RequiredAttribute.cs index fae84f1..244419c 100644 --- a/DynamORM/Validation/RequiredAttribute.cs +++ b/DynamORM/Validation/RequiredAttribute.cs @@ -120,12 +120,12 @@ namespace DynamORM.Validation { int? cnt = null; - var list = (val as IEnumerable); + var list = val as IEnumerable; if (list != null) cnt = list.Count(); else { - var enumerable = (val as IEnumerable); + var enumerable = val as IEnumerable; if (enumerable != null) cnt = enumerable.Cast().Count(); } diff --git a/DynamORM/Validation/ValidationResult.cs b/DynamORM/Validation/ValidationResult.cs index 76631dd..385bf45 100644 --- a/DynamORM/Validation/ValidationResult.cs +++ b/DynamORM/Validation/ValidationResult.cs @@ -13,5 +13,8 @@ namespace DynamORM.Validation /// Gets the value that is broken. public object Value { get; internal set; } + + /// Gets the result. + public ValidateResult Result { get;internal set;} } } \ No newline at end of file