/// <summary> /// Gets the model for this DataReader's currents result /// </summary> /// <param name="reader">the reader having a current result open</param> /// <param name="targetType">the targetType into which to convert the reader's value</param> /// <param name="rules">a set of rules that applies to the current type</param> /// <param name="fieldNames">a set of names provided by the current reader</param> /// <returns>an instance representing the value of the current result of the reader</returns> internal static object GetModel(this IDataReader reader, Type targetType, MapRule[] rules, string[] fieldNames) { object retVal = targetType.GetConstructor(Type.EmptyTypes).Invoke(null); for (int i = 0; i < rules.Length; i++) { MapRule r = rules[i]; if (r != null && fieldNames.Any(n => n.Equals(r.FieldName, StringComparison.OrdinalIgnoreCase))) { try { object val = reader[r.FieldName]; if (!r.UseExpression) { r[retVal] = val; } else { r[retVal] = ProcessResolveExpression(r.ValueResolveExpression, val); } } catch (Exception ex) { LogEnvironment.LogEvent($"Error during model bind! {ex.OutlineException()}", LogSeverity.Warning); } } else if (r != null) { LogEnvironment.LogDebugEvent($"No Value found for MapRule {r.FieldName} ({r.UseExpression};{r.ValueResolveExpression}", LogSeverity.Report); } } return(retVal); }
/// <summary> /// Maps the data of the dynamicResult instance into the T - Instance /// </summary> /// <param name="item">the item that contains the original data</param> /// <param name="target">the target instance into which the data is mapped</param> /// <param name="modelType">the meta-type used to identify mapped columns</param> public static void ToModel(this DynamicResult item, object target, Type modelType) { string[] fieldNames = item.GetDynamicMemberNames().ToArray(); MapRule[] rules = DbColumnAttribute.GetRules(modelType).Clone() as MapRule[]; for (int i = 0; i < rules.Length; i++) { MapRule r = rules[i]; if (r != null && fieldNames.Any(n => n.Equals(r.FieldName, StringComparison.OrdinalIgnoreCase)))//Array.IndexOf(fieldNames, r.FieldName.ToLower()) != -1)))) { try { object val = item[r.FieldName]; if (!r.UseExpression) { r[target] = val; } else { r[target] = ProcessResolveExpression(r.ValueResolveExpression, val); } } catch (Exception ex) { LogEnvironment.LogEvent(string.Format("Failed to set value{1}{0}", ex.OutlineException(), Environment.NewLine), LogSeverity.Warning, "DataAccess"); } } else if (r != null) { LogEnvironment.LogDebugEvent($"No Value found for MapRule {r.FieldName} ({r.UseExpression};{r.ValueResolveExpression}", LogSeverity.Report); } } }