/// <summary> /// Initializes a new instance of the MapRule class /// </summary> /// <param name="targetMember">the targeetMember that is mapped</param> /// <param name="attribute">the attribute </param> public MapRule(MemberInfo targetMember, DbColumnAttribute attribute) { memberName = targetMember.Name; this.dataColumn = attribute; mappings = new Dictionary <Type, MemberInfo>(); mappings.Add(targetMember.DeclaringType, targetMember); }
/// <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); } } }
/// <summary> /// Returns an enuerable with entites representing all values of this reader /// </summary> /// <param name="data">the data that was fetched from the underlaying datasource</param> /// <param name="targetType">the targetType into which to convert the reader's value</param> /// <param name="metaType">the type delcaring meta - information that is capable to assign data on instances of the targetType</param> /// <returns>an enuerable representing all items</returns> public static IEnumerable <object> GetModelResult(this DynamicResult[] data, Type targetType, Type metaType) { MapRule[] rules = DbColumnAttribute.GetRules(metaType).Clone() as MapRule[]; foreach (DynamicResult result in data) { object retVal = targetType.GetConstructor(Type.EmptyTypes).Invoke(null); result.ToModel(retVal, metaType); yield return(retVal); } }
/// <summary> /// Finds the attribute that is bound to a specific member /// </summary> /// <param name="info">the member info that is </param> /// <returns></returns> private static DbColumnAttribute GetDbcAttribute(MemberInfo info) { if ((info is PropertyInfo pi && !pi.CanWrite) || (info is FieldInfo fi && fi.IsInitOnly)) { return(new DbColumnAttribute(false)); } Attribute retVal = Attribute.GetCustomAttribute(info, typeof(DbColumnAttribute)) ?? Attribute.GetCustomAttribute(info, typeof(ColumnAttribute)); if (retVal == null) { retVal = new DbColumnAttribute(info.Name); } if (retVal is ColumnAttribute ca) { retVal = new DbColumnAttribute(ca.Name ?? info.Name); } return((DbColumnAttribute)retVal); }
/// <summary> /// Returns an enuerable with entites representing all values of this reader /// </summary> /// <param name="reader">the reader from which to fetch all results</param> /// <param name="targetType">the targetType into which to convert the reader's value</param> /// <param name="metaType">the type delcaring meta - information that is capable to assign data on instances of the targetType</param> /// <returns>an enuerable representing all items</returns> public static IEnumerable <object> GetModelResult(this IDataReader reader, Type targetType, Type metaType) { MapRule[] rules = DbColumnAttribute.GetRules(metaType).Clone() as MapRule[]; string[] fieldNames = new string[reader.FieldCount]; for (int i = 0; i < fieldNames.Length; i++) { fieldNames[i] = reader.GetName(i).ToLower(); } try { while (reader.Read()) { yield return(reader.GetModel(targetType, rules, fieldNames)); } } finally { reader.Dispose(); } }
/// <summary> /// Returns an enuerable with entites representing all values of this reader /// </summary> /// <typeparam name="T">the target type in which to convert all items</typeparam> /// <typeparam name="TM">the Mapping Type from which to use the mapping for the results</typeparam> /// <param name="reader">the reader from which to fetch all results</param> /// <returns>an enuerable representing all items</returns> public static IEnumerable <T> GetModelResult <T, TM>(this IDataReader reader) where T : new() { MapRule[] rules = DbColumnAttribute.GetRules(typeof(TM)).Clone() as MapRule[]; string[] fieldNames = new string[reader.FieldCount]; for (int i = 0; i < fieldNames.Length; i++) { fieldNames[i] = reader.GetName(i).ToLower(); } try { while (reader.Read()) { yield return(reader.GetModel <T>(rules, fieldNames)); } } finally { reader.Dispose(); } }