Example #1
0
        /// <summary>
        ///     The set calculated properties values.
        /// </summary>
        /// <param name="entity">
        ///     The entity.
        /// </param>
        /// <param name="calculatedValueExpression">
        ///     The calculated value expression.
        /// </param>
        private void SetCalculatedPropertiesValues(T entity, Tuple <object, object> calculatedValueExpression)
        {
            dynamic func            = calculatedValueExpression.Item2;
            object  funcResult      = func.Invoke(entity);
            var     valueExpression = new Tuple <object, object>(calculatedValueExpression.Item1, funcResult);

            ClausesHelper.SetPropertiesValues(entity, valueExpression, false);
        }
Example #2
0
        /// <summary>
        ///     The build entities.
        /// </summary>
        /// <param name="table">
        ///     The table.
        /// </param>
        /// <returns>
        ///     The <see cref="System.Collections.Generic.List{T}" />.
        /// </returns>
        public List <T> BuildEntities(Table table)
        {
            var parsedTree       = new TreeParser(table, this.collectionIncludes.GetHierarchyPropertyNames()).Parse();
            var entityDictionary = new Dictionary <int, object>();

            // Entidades de primer nivel
            var entities = new List <T>();

            for (var i = 0; i < table.RowCount; i++)
            {
                if (parsedTree[i].HierarchyLevel != 0)
                {
                    continue;
                }

                var entity = (T)Helpers.CreateInstance(table, table.Rows[i], typeof(T));
                this.CreateIncludedEntities(entity, table, table.Rows[i]);

                Helpers.ReplaceDateTimeMinimumValues(entity);

                // Set Fixed values
                foreach (var fixedValueExpression in this.fixedValueExpressions)
                {
                    ClausesHelper.SetPropertiesValues(entity, fixedValueExpression, true);
                }

                // Set Default values
                foreach (var defaultValueExpression in this.defaultValueExpressions)
                {
                    ClausesHelper.SetPropertiesValues(entity, defaultValueExpression, false);
                }

                // Set calculated values
                foreach (var calculatedValueExpression in this.calculatedValueExpressions)
                {
                    this.SetCalculatedPropertiesValues(entity, calculatedValueExpression);
                }

                this.dbContext.Set <T>().Add(entity);
                this.CreateIncludedCollections1(entity, table.Rows[i], table.Header);

                entities.Add(entity);

                entityDictionary.Add(i, entity);
            }

            // Resto de la jerarquia + collectionincludes
            var maxHierarchyLevel = parsedTree.Max(x => x.Value.HierarchyLevel);

            for (var hierarchyLevel = 1; hierarchyLevel <= maxHierarchyLevel; hierarchyLevel++)
            {
                var indexes =
                    parsedTree.Where(x => x.Value.HierarchyLevel == hierarchyLevel).Select(x => x.Key).ToList();

                for (var i = 0; i < table.RowCount; i++)
                {
                    if (!indexes.Contains(i))
                    {
                        continue;
                    }

                    var rowInfo = parsedTree[i];

                    // Look for the parent and add the entity to the parent
                    var parentRowInfo = rowInfo.Parent;
                    var parentIndex   = parsedTree.Single(x => x.Value == parentRowInfo).Key;
                    var parentEntity  = entityDictionary[parentIndex];

                    // Add the child entity or included collection
                    this.CreateIncludedCollections2((T)parentEntity, table.Rows[i], table.Header, rowInfo.RowKey);
                }
            }

            return(entities);
        }