private object InternalTranslate(Type type, DataTranslationItem item) { ConstructorInfo constructor = type.GetConstructor(new Type[0]); var target = (Entity)constructor.Invoke(new object[0]); return(InternalTranslate(target, item)); }
public virtual IEnumerable <Entity> ExecuteQuery(Entity dataContainer, DbCommand command) { ICollection <Entity> result = null; DbDataReader reader = null; try { if (command.Connection.State != System.Data.ConnectionState.Open) { command.Connection.Open(); } reader = command.ExecuteReader(); result = new List <Entity>(); do { DataTranslationItem item = new DataTranslationItem(reader.FieldCount); for (var i = 0; i < reader.FieldCount; i++) { item.DataTypeNames[i] = reader.GetDataTypeName(i); item.Types[i] = reader.GetFieldType(i); item.ColumnNames[i] = reader.GetName(i); } while (reader.Read()) { for (var i = 0; i < reader.FieldCount; i++) { item.ResultSet[i] = reader.GetValue(i); } result.Add(InternalTranslate(dataContainer, item)); } }while (reader.NextResult()); int _recordsAffected = reader.RecordsAffected; } catch { throw; } finally { if (reader != null) { reader.Dispose(); } // Releasing connection resource DisposeCommand(command); } return(result); }
public virtual IEnumerable <T> ExecuteQuery <T>(DbCommand command) where T : Entity { ICollection <T> result = null; DbDataReader reader = null; try { if (command.Connection.State != System.Data.ConnectionState.Open) { command.Connection.Open(); } reader = command.ExecuteReader(); result = new List <T>(); do { DataTranslationItem item = new DataTranslationItem(reader.FieldCount); for (var i = 0; i < reader.FieldCount; i++) { item.DataTypeNames[i] = reader.GetDataTypeName(i); item.Types[i] = reader.GetFieldType(i); item.ColumnNames[i] = reader.GetName(i); } while (reader.Read()) { for (var i = 0; i < reader.FieldCount; i++) { item.ResultSet[i] = reader.GetValue(i); } result.Add(InternalTranslate <T>(item)); } }while (reader.NextResult()); int _recordsAffected = reader.RecordsAffected; } catch { throw; } finally { if (reader != null) { reader.Dispose(); } if (command.Connection != null && command.Connection.State != System.Data.ConnectionState.Closed) { command.Connection.Close(); } if (command.Connection != null) { command.Connection.Dispose(); } if (command != null) { command.Dispose(); } } return(result); }
private Entity InternalTranslate(Entity target, DataTranslationItem item) { Type type = target.GetType(); for (int j = 0; j < item.Size; j++) { object value = item.ResultSet[j]; if (value is DBNull) { value = null; } string name = item.ColumnNames[j]; bool isForeignKey = false; var property = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (property == null && name != "ID" && name.Contains("ID")) { name = name.Replace("ID", ""); isForeignKey = true; property = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); } if (property != null && property.CanWrite) { if (isForeignKey) { var propertyValue = property.GetValue(target); if (propertyValue != null && typeof(Entity).IsAssignableFrom(propertyValue.GetType())) { Entity complexField = (Entity)propertyValue; if (value != null) { complexField.ID = (Guid)value; } else { complexField = null; } property.SetValue(target, complexField); } else if (property.PropertyType.IsInterface && typeof(Entity).IsAssignableFrom(property.PropertyType)) { // Removing I from interface name string strongTypeName = property.PropertyType.AssemblyQualifiedName; string[] parts = strongTypeName.Split(','); int index = parts[0].LastIndexOf('.'); parts[0] = parts[0].Remove(index + 1, 1); strongTypeName = String.Join(",", parts); // Creating instance of the concrete type Type concreteType = Type.GetType(strongTypeName); ConstructorInfo constructor = concreteType.GetConstructor(new Type[0]); var strongTypeValue = (Entity)constructor.Invoke(new object[0]); if (strongTypeValue != null) { Entity complexField = (Entity)strongTypeValue; if (value != null) { complexField.ID = (Guid)value; // if the property was null then it was not intended to pull the entire // object for this property, rather we should only fetch ID complexField.SetIgnored(true); } else { complexField = null; } property.SetValue(target, complexField); } else { property.SetValue(target, null); } } else { property.SetValue(target, null); } } else if (property.PropertyType == typeof(Double)) { property.SetValue(target, Convert.ToDouble(value)); } else { SetValueWithNullableEnumCheck(property, target, value); } } } return((Entity)target); }
private T InternalTranslate <T>(DataTranslationItem item) where T : Entity { Type type = typeof(T); return((T)InternalTranslate(type, item)); }