Exemple #1
0
        /// <summary>
        /// 填充实体
        /// </summary>
        /// <param name="table">需要转换实体的 LiteDataTable</param>
        /// <param name="repo">实体的仓储</param>
        /// <param name="propertyToColumnMappings">属性和对应列的键值对集合</param>
        /// <param name="columnMapToProperty">
        /// 此参数表示表格中的列的数据类型和是否直接映射实体的属性数据类型。
        /// 如果传入 false,表示表格中的列数据类型和实体属性数据类型不一致。
        /// </param>
        /// <returns></returns>
        private static EntityList ConvertEntitiesIntoList(
            LiteDataTable table,
            EntityRepository repo,
            IList <PropertyToColumnMapping> propertyToColumnMappings,
            bool columnMapToProperty = true
            )
        {
            var resultList = repo.NewList();

            var rdbDataProvider = ORM.RdbDataProvider.Get(repo);
            var columns         = rdbDataProvider.DbTable.Columns;

            for (int i = 0, c = table.Rows.Count; i < c; i++)
            {
                var row = table.Rows[i];

                var entityItem = repo.New();

                for (int j = 0, c2 = propertyToColumnMappings.Count; j < c2; j++)
                {
                    var mapping  = propertyToColumnMappings[j];
                    var rowValue = row[mapping.ColumnIndex];
                    if (!columnMapToProperty)
                    {
                        for (int z = 0, z2 = columns.Count; z < z2; z++)
                        {
                            var column = columns[z];
                            //通过列名称相等匹配找到 rdbcolumn。
                            if (column.Name.EqualsIgnoreCase(mapping.ColumnName))
                            {
                                column.WritePropertyValue(entityItem, rowValue);
                                break;
                            }
                        }
                    }
                    else
                    {
                        entityItem.LoadProperty(mapping.Property, rowValue);
                    }
                }

                resultList.Add(entityItem);
            }

            return(resultList);
        }
        /// <summary>
        /// 填充实体
        /// </summary>
        /// <param name="liteDataTable">需要转换实体的 LiteDataTable</param>
        /// <param name="repo">实体的仓储</param>
        /// <param name="propertyToColumnMappings">属性和对应列的键值对集合</param>
        /// <returns></returns>
        private static EntityList ConvertEntitiesIntoList(LiteDataTable liteDataTable, EntityRepository repo, IList <PropertyToColumnMapping> propertyToColumnMappings)
        {
            var resultList = repo.NewList();

            for (int i = 0, c = liteDataTable.Rows.Count; i < c; i++)
            {
                var row = liteDataTable.Rows[i];

                var entityItem = repo.New();
                for (int j = 0, c2 = propertyToColumnMappings.Count; j < c2; j++)
                {
                    var mapping  = propertyToColumnMappings[j];
                    var rowValue = row[mapping.ColumnName];

                    entityItem.LoadProperty(mapping.Property, rowValue);
                }

                resultList.Add(entityItem);
            }

            return(resultList);
        }
Exemple #3
0
        /// <summary>
        /// 重新设置整个表的所有 TreeIndex。
        /// 注意,此方法只保证生成的 TreeIndex 有正确的父子关系,同时顺序有可能被打乱。
        /// </summary>
        /// <param name="repository"></param>
        public static void ResetTreeIndex(EntityRepository repository)
        {
            using (var tran = RF.TransactionScope(repository))
            {
                //先清空
                var dp     = repository.RdbDataProvider;
                var table  = dp.DbTable;
                var column = table.Columns.First(c => (c as DbColumn).Property == Entity.TreeIndexProperty);
                using (var dba = dp.CreateDbAccesser())
                {
                    dba.ExecuteText(string.Format("UPDATE {0} SET {1} = NULL", table.Name, column.Name));
                }

                var all = repository.GetAll();
                if (all.Count > 0)
                {
                    //如果加载的过程中,第一个节点刚好是根节点,
                    //则加载完成后是一棵完整的树,Index 也生成完毕,不需要再次处理。
                    if (all.IsTreeRootList)
                    {
                        all.ResetTreeIndex();
                        repository.Save(all);
                    }
                    else
                    {
                        var cloneOptions = CloneOptions.ReadSingleEntity();
                        var oldList      = new List <Entity>();
                        all.EachNode(e =>
                        {
                            var cloned = repository.New();
                            cloned.Clone(e, cloneOptions);
                            cloned.PersistenceStatus = PersistenceStatus.Unchanged;

                            oldList.Add(cloned);
                            return(false);
                        });

                        var newList = repository.NewList();
                        while (oldList.Count > 0)
                        {
                            foreach (var item in oldList)
                            {
                                var treePId = item.TreePId;
                                if (treePId == null)
                                {
                                    newList.Add(item);
                                    oldList.Remove(item);
                                    break;
                                }
                                else
                                {
                                    var parent = newList.EachNode(e => e.Id.Equals(treePId));
                                    if (parent != null)
                                    {
                                        parent.TreeChildren.LoadAdd(item);
                                        oldList.Remove(item);
                                        break;
                                    }
                                }
                            }
                        }
                        TreeHelper.MarkTreeFullLoaded(newList);
                        newList.ResetTreeIndex();
                        repository.Save(newList);
                    }
                }

                tran.Complete();
            }
        }