Exemplo n.º 1
0
        public NestedEntities BuildNestedEntities(NestedLines nestedLines, String entityName, NestedEntity parent, int level = 1)
        {
            var nestedEntities = new NestedEntities();

            foreach (var line in nestedLines.Lines)
            {
                var idField = line.GetId(entityName, level);

                if (idField == null || !_entitiesList.ContainsKey(entityName))
                {
                    continue;
                }

                var entityType = _entitiesList[entityName];

                NestedEntity nestedEntity = new NestedEntity(idField.Value.ToString(), entityName, entityType);

                nestedEntity.NestedFields.AddRange(line.Fields.Where(x => x.Name.Length == level + 1).ToList());

                if (parent != null)
                {
                    parent.AddChild(nestedEntity.EntityName, nestedEntity);
                }

                var fieldsByEntity = line.GroupFieldsByEntity(level + 1);

                foreach (var item in fieldsByEntity)
                {
                    if (!_entitiesList.ContainsKey(item.Key))
                    {
                        continue;
                    }

                    BuildNestedEntities(new NestedLines(new List <NestedLine> {
                        new NestedLine(item.ToList())
                    }), item.Key, nestedEntity, level + 1);
                }

                nestedEntities.Add(nestedEntity);
            }

            return(nestedEntities);
        }
Exemplo n.º 2
0
        public List <object> BuildEntities(NestedEntities entities)
        {
            List <object> returnEntities = new List <object>();

            foreach (var nestedEntity in entities.Entities)
            {
                var entity = Activator.CreateInstance(nestedEntity.EntityType);

                var properties = nestedEntity.EntityType.GetProperties();

                foreach (var field in nestedEntity.NestedFields)
                {
                    var currentProp = properties.FirstOrDefault(x => x.Name.Equals(field.PropertyName(), StringComparison.InvariantCultureIgnoreCase));

                    if (currentProp == null)
                    {
                        continue;
                    }

                    if (currentProp.PropertyType.GUID != field.Type.GUID)
                    {
                        continue;
                    }

                    currentProp.SetValue(entity, field.Value);
                }

                foreach (var child in nestedEntity.Childs)
                {
                    var childProperty = properties.FirstOrDefault(x => x.Name.Equals(child.Key, StringComparison.InvariantCultureIgnoreCase));

                    if (childProperty is null)
                    {
                        continue;
                    }

                    if (childProperty.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))
                    {
                        var genericArgumentGuid = childProperty.PropertyType.GetGenericArguments().First().GUID;

                        if (genericArgumentGuid != child.Value.EntityType.GUID)
                        {
                            continue;
                        }

                        var childEntities = BuildEntities(new NestedEntities().Add(child.Value));

                        object instance = Activator.CreateInstance(childProperty.PropertyType);

                        IList list = (IList)instance;

                        foreach (var item in childEntities)
                        {
                            list.Add(item);
                        }

                        childProperty.SetValue(entity, list);
                    }
                    else
                    {
                        childProperty.SetValue(entity, BuildEntities(new NestedEntities().Add(child.Value)).FirstOrDefault());
                    }
                }

                returnEntities.Add(entity);
            }

            return(returnEntities);
        }