/// <summary> /// Convierte una Tabla de datos (System.Data.Datatable) a una tabla de entidades del Framework CORFO del tipo T /// </summary> /// <typeparam name="T">Tipo de Entidad a Convertir</typeparam> /// <param name="TableSource">Extension de Datatable</param> /// <returns></returns> public static Gale.Db.EntityTable <T> ConvertToEntityTable <T>(this System.Data.DataTable TableSource) where T : class, new() { Type objectToConvertType = typeof(T); //Perform Pattern For Huge Data =) List <MemoryFieldCaching> MemoryOptimizer = null; Gale.Db.EntityTable <T> entityTable = new Gale.Db.EntityTable <T>(); MemoryOptimizer = (from t in objectToConvertType.GetProperties() where t.CanRead && t.GetIndexParameters().Count() == 0 && (t.PropertyType.IsGenericType == false || (t.PropertyType.IsGenericType == true && t.PropertyType.GetGenericTypeDefinition() != typeof(System.Data.Linq.EntitySet <>))) select new MemoryFieldCaching { columnName = t.Name, property = t, columnAttribute = (System.Data.Linq.Mapping.ColumnAttribute)t.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).FirstOrDefault(), }).ToList(); foreach (System.Data.DataRow row in TableSource.Rows) { T objectMapped = new T(); foreach (MemoryFieldCaching Caching in MemoryOptimizer) { //Perform Pattern For Huge Data =) if (TableSource.Columns.Contains(Caching.columnName)) { if (Caching.ordinal == -1) { Caching.ordinal = TableSource.Columns[Caching.columnName].Ordinal; } object value = row[Caching.ordinal]; if (row[Caching.ordinal] != System.DBNull.Value) { //Cast To Type if (Caching.property.PropertyType == typeof(char)) //Char { value = System.Char.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(Int16)) //Int16 { value = System.Int16.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(Int32)) //Int32 { value = System.Int32.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(Int64)) //Int64 { value = System.Int64.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(Decimal)) //Decimal { value = System.Decimal.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(string)) //String { value = row[Caching.ordinal].ToString(); } else if (Caching.property.PropertyType == typeof(DateTime)) //DateTime { value = System.DateTime.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(Byte)) //Byte { value = System.Byte.Parse(value.ToString()); } else if (Caching.property.PropertyType == typeof(System.Guid)) //Byte { value = System.Guid.Parse(value.ToString()); } else { value = row[Caching.ordinal]; } } else { value = null; } Caching.property.SetValue(objectMapped, value, null); } } entityTable.Add(objectMapped); } return(entityTable); }
/// <summary> /// Fill a T model with the Database Result Values /// </summary> /// <typeparam name="T">Type to fill</typeparam> /// <param name="model">Entity Table to Fill</param> /// <param name="table">DB Result Table</param> private void FillEntity <T>(ref Gale.Db.EntityTable <T> model, System.Data.DataTable table) { Type EntityType = typeof(T); //Perform Pattern For Huge Data =) List <MemoryFieldCaching> MemoryOptimizer = (from t in EntityType.GetProperties() where t.CanRead && t.GetIndexParameters().Count() == 0 && (t.PropertyType.IsGenericType == false || (t.PropertyType.IsGenericType == true && t.PropertyType.GetGenericTypeDefinition() != typeof(System.Data.Linq.EntitySet <>))) select new MemoryFieldCaching { columnName = t.Name, property = t }).ToList(); foreach (System.Data.DataRow row in table.Rows) { T Item = Activator.CreateInstance <T>(); #region Transform Each Row foreach (MemoryFieldCaching Caching in MemoryOptimizer) { //Perform Pattern For Huge Data =) //Ordinal: // -1: Significa que tiene que ir a buscarlo string Name = ""; if (Caching.ordinal == -1) { try { Name = Caching.columnName; System.Data.Linq.Mapping.ColumnAttribute ColumnAttribute = (System.Data.Linq.Mapping.ColumnAttribute)(Caching.property.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), true).FirstOrDefault()); if (ColumnAttribute != null && ColumnAttribute.Name != null && ColumnAttribute.Name.Length > 0) { Name = ColumnAttribute.Name; } if (ColumnAttribute == null) { Caching.ordinal = -2; //Campo Personalizado (no debe ser llenado por base de datos) } else { if (ColumnAttribute != null) { //Si existe el atributo de columna y puede ser nulo, verifico que esta columna exista, de no existir esta columna, //no se debe caer, sino que solo no debe establecerla if (ColumnAttribute.CanBeNull || ColumnAttribute.DbType == null) { if (table.Columns.Contains(Name)) { Caching.ordinal = table.Columns[Name].Ordinal; } else { Caching.ordinal = -2; } } else { try { if (table.Columns.Contains(Name)) { Caching.ordinal = table.Columns[Name].Ordinal; } else { Caching.ordinal = -2; } } catch (System.Exception ex) { //throw new Gale.Gale.Exception.GaleException("ColumnNameNotFoundInDataServiceAndIsNotNullable", Caching.columnName, Name, EntityType.Name); throw ex; } } } else { Caching.ordinal = table.Columns[Name].Ordinal; } } } catch (System.Exception ex) { //---[ Guard Exception ]------------------------------------------------------------------------------------------------------- Gale.Exception.GaleException.Guard(() => ex is IndexOutOfRangeException, "ColumnNameNotFoundInDataService", Caching.columnName, Name, EntityType.Name); //----------------------------------------------------------------------------------------------------------------------------- throw ex; } } if (Caching.ordinal != -2) { object data = row[Caching.ordinal]; if (data is DateTime) { data = DateTime.SpecifyKind((DateTime)data, DateTimeKind.Local); } if (data is System.Guid && Caching.property.PropertyType == typeof(String)) { data = data.ToString(); } if (!(data is System.DBNull)) { //Parse AnyWay for implicit Casting if (Caching.property.PropertyType.IsGenericType == false && Caching.property.PropertyType != data.GetType()) { data = Convert.ChangeType(data, Caching.property.PropertyType); } Caching.property.SetValue(Item, data, null); } } } #endregion model.Add(Item); } }