This commit is contained in:
grzegorz.russek
2018-09-28 17:38:06 +00:00
parent 913d29274e
commit dae78d21de
5 changed files with 221 additions and 4 deletions

View File

@@ -1499,6 +1499,35 @@ namespace DynamORM
return (T)mapper.Create(item);
}
/// <summary>MapEnumerable object enumerator into specified type using property names.</summary>
/// <typeparam name="T">Type to which columnMap results.</typeparam>
/// <param name="enumerable">Source enumerator.</param>
/// <returns>Enumerator of specified type.</returns>
public static IEnumerable<T> MapEnumerableByProperty<T>(this IEnumerable<object> enumerable)
{
DynamicTypeMap mapper = DynamicMapperCache.GetMapper<T>();
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);
}
/// <summary>MapEnumerable object item into specified type using property names.</summary>
/// <typeparam name="T">Type to which columnMap results.</typeparam>
/// <param name="item">Source object.</param>
/// <returns>Item of specified type.</returns>
public static T MapByProperty<T>(this object item)
{
DynamicTypeMap mapper = DynamicMapperCache.GetMapper<T>();
if (mapper == null)
throw new InvalidOperationException("Type can't be mapped for unknown reason.");
return (T)mapper.CreateByProperty(item);
}
/// <summary>Fill object of specified type with data from source object.</summary>
/// <typeparam name="T">Type to which columnMap results.</typeparam>
/// <param name="item">Item to which columnMap data.</param>
@@ -1516,6 +1545,23 @@ namespace DynamORM
return item;
}
/// <summary>Fill object of specified type with data from source object using property names.</summary>
/// <typeparam name="T">Type to which columnMap results.</typeparam>
/// <param name="item">Item to which columnMap data.</param>
/// <param name="source">Item from which extract data.</param>
/// <returns>Filled item.</returns>
public static T FillByProperty<T>(this T item, object source)
{
DynamicTypeMap mapper = DynamicMapperCache.GetMapper<T>();
if (mapper == null)
throw new InvalidOperationException("Type can't be mapped for unknown reason.");
mapper.MapByProperty(item, source);
return item;
}
/// <summary>MapEnumerable object enumerator into specified type.</summary>
/// <param name="enumerable">Source enumerator.</param>
/// <param name="type">Type to which columnMap results.</param>
@@ -1545,6 +1591,35 @@ namespace DynamORM
return mapper.Create(item);
}
/// <summary>MapEnumerable object enumerator into specified type using property names.</summary>
/// <param name="enumerable">Source enumerator.</param>
/// <param name="type">Type to which columnMap results.</param>
/// <returns>Enumerator of specified type.</returns>
public static IEnumerable<object> MapEnumerableByProperty(this IEnumerable<object> 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);
}
/// <summary>MapEnumerable object item into specified type using property names.</summary>
/// <param name="item">Source object.</param>
/// <param name="type">Type to which columnMap results.</param>
/// <returns>Item of specified type.</returns>
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);
}
/// <summary>Converts the elements of an <see cref="System.Collections.IEnumerable"/>
/// to the specified type.</summary>
/// <typeparam name="T">The type to convert the elements of source to.</typeparam>

View File

@@ -122,6 +122,15 @@ namespace DynamORM.Mapper
return Map(source, Creator());
}
/// <summary>Create object of <see cref="DynamicTypeMap.Type"/> type and fill values from <c>source</c> using property names.</summary>
/// <param name="source">Object containing values that will be mapped to newly created object.</param>
/// <returns>New object of <see cref="DynamicTypeMap.Type"/> type with matching values from <c>source</c>.</returns>
public object CreateByProperty(object source)
{
return MapByProperty(source, Creator());
}
/// <summary>Fill values from <c>source</c> to <see cref="DynamicTypeMap.Type"/> object in <c>destination</c>.</summary>
/// <param name="source">Object containing values that will be mapped to newly created object.</param>
/// <param name="destination">Object of <see cref="DynamicTypeMap.Type"/> type to which copy values from <c>source</c>.</param>
@@ -140,6 +149,26 @@ namespace DynamORM.Mapper
return destination;
}
/// <summary>Fill values from <c>source</c> to <see cref="DynamicTypeMap.Type"/> object in <c>destination</c> using property names.</summary>
/// <param name="source">Object containing values that will be mapped to newly created object.</param>
/// <param name="destination">Object of <see cref="DynamicTypeMap.Type"/> type to which copy values from <c>source</c>.</param>
/// <returns>Object of <see cref="DynamicTypeMap.Type"/> type with matching values from <c>source</c>.</returns>
public object MapByProperty(object source, object destination)
{
string cn = null;
DynamicPropertyInvoker dpi = null;
foreach (KeyValuePair<string, object> 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;
}
/// <summary>Validates the object.</summary>
/// <param name="val">The value.</param>
/// <returns>List of not valid results.</returns>
@@ -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,
});
}
}

View File

@@ -120,12 +120,12 @@ namespace DynamORM.Validation
{
int? cnt = null;
var list = (val as IEnumerable<object>);
var list = val as IEnumerable<object>;
if (list != null)
cnt = list.Count();
else
{
var enumerable = (val as IEnumerable);
var enumerable = val as IEnumerable;
if (enumerable != null)
cnt = enumerable.Cast<object>().Count();
}

View File

@@ -13,5 +13,8 @@ namespace DynamORM.Validation
/// <summary>Gets the value that is broken.</summary>
public object Value { get; internal set; }
/// <summary>Gets the result.</summary>
public ValidateResult Result { get;internal set;}
}
}