public static T QueryForObject <T>(IDataReader dr) where T : new() { Logger.Debug("Begin method"); var entityType = typeof(T); var entity = default(T); try { var properties = CfMapCache.GetInstance(entityType); if (dr.Read()) { entity = new T(); for (var index = 0; index < dr.FieldCount; index++) { if (properties.ContainsKey(dr.GetName(index).ToUpper())) { var info = properties[dr.GetName(index).ToUpper()]; if (info != null && info.CanWrite && !dr.IsDBNull(index)) { try { info.SetValue(entity, dr.GetValue(index), null); } catch (Exception ex) { Logger.Error(ex); throw new CfException(String.Format("Conversion error in {0}.{1} - (Connection Factory: QueryForObject) > {2}", entityType.Name, dr.GetName(index), ex.Message), ex); } } } else { Logger.Debug("Property not exist: " + entityType.FullName + "." + dr.GetName(index)); } } } } catch (Exception ex) { Logger.Error(ex); throw new CfException("Unknown Error (Connection Factory: QueryForObject) " + ex.Message, ex); } finally { dr.Close(); } Logger.Debug("End method"); return(entity); }
public static IEnumerable <T> LazyLoadForObjects <T>(IEnumerable <IDataReader> dr) where T : new() { Logger.Debug("Begin method"); Type entityType = typeof(T); var properties = CfMapCache.GetInstance(entityType); using (var enumerator = dr.GetEnumerator()) { while (enumerator.MoveNext()) { var reader = enumerator.Current; if (reader != null) { var newObject = new T(); for (var index = 0; index < reader.FieldCount; index++) { if (properties.ContainsKey(reader.GetName(index).ToUpper())) { var info = properties[reader.GetName(index).ToUpper()]; if ((info != null) && info.CanWrite && !reader.IsDBNull(index)) { try { info.SetValue(newObject, reader.GetValue(index), null); } catch (Exception ex) { Logger.Error(ex); throw new CfException(String.Format("Conversion error in {0}.{1} - (Connection Factory: LazyLoadForObjects) > {2}", entityType.Name, reader.GetName(index), ex.Message), ex); } } } else { Logger.Debug("Property not exist: " + entityType.FullName + reader.GetName(index)); } } yield return(newObject); } } } Logger.Debug("End method"); }