示例#1
0
        /// <summary>
        /// Fills an entity list provided
        /// </summary>
        /// <param name="dataSet">The dataset with the data</param>
        /// <param name="entityList">The entity list to populate</param>
        /// <returns></returns>
        static object FillEntityList(DataSet dataSet, int currentTable, object entityList)
        {
            // Generate the column map for the current table
            Type entityType = entityList.GetEntityTypeFromEntityList();

            Dictionary <Type, ColumnPropertyMap> entityColumnMaps = ColumnPropertyMap.CreateMapsForEntity(dataSet.Tables[currentTable], entityType);

            // For each row in the table, try to populate an entity
            for (int r = 0; r < dataSet.Tables[currentTable].Rows.Count; r++)
            {
                bool couldFillEntity = false;

                // Create an entity
                object entity = ObjectBuilder.Create(entityType);

                // Fill all non-entity properties in the entity with row columns
                couldFillEntity = FillEntity(dataSet, currentTable, r, entity, entityColumnMaps[entityType]);

                // Fill all the entity properties
                foreach (EntityPropertyInfo entityProperty in entity.GetEntityProperties())
                {
                    object containedEntity = ObjectBuilder.Create(entityProperty.PropertyInfo.PropertyType);
                    if (FillEntity(dataSet, currentTable, r, containedEntity, entityColumnMaps[entityProperty.PropertyInfo.PropertyType]))
                    {
                        if (entityProperty.PropertyAccess.CanWrite)
                        {
                            entityProperty.PropertyAccess.Set(entity, containedEntity);
                            couldFillEntity = true;
                        }
                    }
                }

                // Fill all the entity list properties
                foreach (EntityPropertyInfo entityListProperty in entity.GetEntityListProperties())
                {
                    // Get the specified data table for the curret entity list
                    int entityListTable = entityListProperty.DataTable == null ? currentTable : entityListProperty.DataTable.GetEffectiveDataTable(currentTable);

                    // If a data table index is explicitly specified load the data off of that
                    // In case we have an entity list but there is no table specified
                    // we can't do much.
                    if (entityListTable != currentTable)
                    {
                        // Load the entity list
                        object containedEntityList = ObjectBuilder.Create(entityListProperty.PropertyInfo.PropertyType);

                        // Fill it based on the table specified
                        FillEntityList(dataSet, entityListTable, containedEntityList);

                        // ... and finaly save it back
                        if (entityListProperty.PropertyAccess.CanWrite)
                        {
                            entityListProperty.PropertyAccess.Set(entity, containedEntityList);
                        }
                    }
                }

                if (couldFillEntity)
                {
                    ((IList)entityList).Add(entity);
                }
            }

            return(entityList);
        }