Exemplo n.º 1
0
 /// <summary>
 /// 搜索并且构建树.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="parentNode">The parent TreeNode.</param>
 /// <returns>IEnumerable&lt;TreeNode&gt;.</returns>
 private IEnumerable <TreeNode> FindTree(ref TreeviewContext context, TreeNode parentNode)
 {
     if (parentNode == null)
     {
         IEnumerable <TreeNode> nodeList = context.TreeNodeSource.Where(m => m.ParentUUID == Guid.Empty);
         var treeRoot = nodeList as IList <TreeNode> ?? nodeList.ToList();
         if (treeRoot.Any())
         {
             foreach (var TreeNode in treeRoot)
             {
                 TreeNode.Level    = 1;
                 TreeNode.Children = FindTree(ref context, TreeNode).OrderBy(m => m.SortSeed).ToList();
             }
         }
         return(treeRoot);
     }
     else
     {
         IEnumerable <TreeNode> menuList = context.TreeNodeSource.Where(m => m.ParentUUID == parentNode.UUID);
         var children = menuList as IList <TreeNode> ?? menuList.ToList();
         if (children.Any())
         {
             foreach (TreeNode TreeNode in children)
             {
                 TreeNode.Level      = parentNode.Level + 1;
                 TreeNode.ParentName = parentNode.Name;
                 TreeNode.Children   = FindTree(ref context, TreeNode).OrderBy(m => m.SortSeed).ToList();
             }
         }
         return(children);
     }
 }
Exemplo n.º 2
0
        static TreeviewContext()
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists <TreeviewContext>());
            TreeviewContext context = new TreeviewContext();

            context.Database.CreateIfNotExists();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        public bool Delete(Guid uuid)
        {
            TreeviewContext context    = new TreeviewContext();
            var             dataSource = context.TreeNodeSource;
            var             oldObj     = dataSource.FirstOrDefault(o => o.UUID == uuid);

            if (oldObj == null)
            {
                return(true);
            }
            List <Guid> needDeleteUUIDs = new List <Guid>();

            if (HasChildren(uuid, ref context))
            {
                GetDeleteChildren(uuid, ref context, ref needDeleteUUIDs);
                if (needDeleteUUIDs.Count > 0)
                {
                    foreach (var id in needDeleteUUIDs)
                    {
                        var tempObj = dataSource.FirstOrDefault(o => o.UUID == id);
                        if (tempObj == null)
                        {
                            continue;
                        }
                        dataSource.Remove(tempObj);
                    }
                }
            }
            dataSource.Remove(oldObj);
            if (context.SaveChanges() > 0)
            {
                return(true);
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 通过对象的属性名和指定的值搜寻对象集合
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IEnumerable <TEntity> FindBy(string propertyName, object value)
        {
            var valueType = value.GetType();

            if (valueType.IsPrimitive || valueType == typeof(string) || valueType == typeof(Guid))
            {
                TreeviewContext context = new TreeviewContext();
                return(context.GetDatasource <TEntity>().Where(o => Common.GetPropertyValue(o, null) == value));
            }
            throw new NoSupportCompareTypeException("不支持查询的属性");
        }
Exemplo n.º 5
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <param name="orderbyCol"></param>
        /// <param name="sortOrder"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static IEnumerable <TEntity> FindPage(int pindex, int psize, string orderbyCol, SortOrder sortOrder, out int total)
        {
            TreeviewContext context = new TreeviewContext();
            //return (from e in DataSource orderby GetPropertyValue(e, orderbyCol) select e).Take(psize);
            var temp = context.GetDatasource <TEntity>()
                       .AsEnumerable()
                       .OrderBy(o => Common.GetPropertyValue(o, orderbyCol));

            total = temp.Count();
            return(temp.Take(pindex * psize).Skip(psize * (pindex - 1)));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Finds the specified UUID.
        /// </summary>
        /// <param name="uuid">The UUID.</param>
        /// <returns>TreeNode.</returns>
        public TreeNode Find(Guid uuid)
        {
            TreeviewContext context  = new TreeviewContext();
            var             TreeNode = context.TreeNodeSource.FirstOrDefault(m => m.UUID == uuid);

            if (TreeNode != null)
            {
                var firstOrDefault = context.TreeNodeSource.FirstOrDefault(m => TreeNode.ParentUUID == m.UUID);
                if (firstOrDefault != null)
                {
                    TreeNode.ParentName = firstOrDefault.Name;
                }
            }
            return(TreeNode);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 通过一个新的对象来更新旧对象,
        /// 具体做法是将新对象中的属性的
        /// 值全部复制到旧对象中更新
        /// </summary>
        /// <param name="newobj"></param>
        /// <returns></returns>
        public static TEntity Update(TEntity newobj)
        {
            TreeviewContext context = new TreeviewContext();
            var             oldObj  = context.GetDatasource <TEntity>().FirstOrDefault(o => o.UUID == newobj.UUID);

            if (oldObj == null)
            {
                throw new Exception("未找到需要更新的元素");
            }
            Common.UpdateObj(ref oldObj, newobj);
            if (context.SaveChanges() > 0)
            {
                return(newobj);
            }
            return(null);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 当删除树节点时,递归查找其子集,一并删除.
        /// </summary>
        /// <param name="parentUUID"></param>
        /// <param name="context"></param>
        /// <param name="needDeleteUUIDs"></param>
        private void GetDeleteChildren(Guid parentUUID, ref TreeviewContext context, ref List <Guid> needDeleteUUIDs)
        {
            var children = context.TreeNodeSource.Where(m => m.ParentUUID == parentUUID).Select(m => m.UUID);

            if (children.Any())
            {
                foreach (Guid child in children)
                {
                    if (HasChildren(child, ref context))
                    {
                        GetDeleteChildren(child, ref context, ref needDeleteUUIDs);
                    }
                }
                needDeleteUUIDs.AddRange(children);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <param name="keyword"></param>
        /// <param name="sortColumn"></param>
        /// <param name="sortOrder"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static IEnumerable <TEntity> FindPage(int pindex, int psize, string keyword, string sortColumn,
                                                     SortOrder sortOrder, out int total)
        {
            TreeviewContext context = new TreeviewContext();

            if (string.IsNullOrEmpty(keyword))
            {
                return(FindPage(pindex, psize, sortColumn, sortOrder, out total));
            }
            var temp = context.GetDatasource <TEntity>()
                       .AsEnumerable()
                       .Where(e => ContansKeyword(e, keyword))
                       .OrderBy(o => Common.GetPropertyValue(o, sortColumn));

            total = temp.Count();
            return(temp.Take(pindex * psize).Skip(psize * (pindex - 1)));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 删除一个元素,如果元素不存在,则直接返回true
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns>
        /// 如果元素不存在,返回true
        /// 删除成功返回true
        /// 如果删除失败,则返回false
        /// </returns>
        public static bool Delete(Guid uuid)
        {
            TreeviewContext context    = new TreeviewContext();
            var             dataSource = context.GetDatasource <TEntity>();
            var             oldObj     = dataSource.FirstOrDefault(o => o.UUID == uuid);

            if (oldObj == null)
            {
                return(true);
            }
            dataSource.Remove(oldObj);
            if (context.SaveChanges() > 0)
            {
                return(true);
            }
            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 通过实体属性名称和其对应的值来搜索并且返回
        /// 指定页码和条数以及排序规则下的数据集合
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <param name="orderbyCol"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        public static IEnumerable <TEntity> FindPageBy(string propertyName, object value, int pindex, int psize,
                                                       string orderbyCol, string sortOrder = "asc")
        {
            var valueType = value.GetType();

            if (valueType.IsPrimitive || valueType == typeof(string) || valueType == typeof(Guid))
            {
                TreeviewContext context = new TreeviewContext();
                return
                    (context.GetDatasource <TEntity>()
                     .Where(o => Common.GetPropertyValue(o, null) == value)
                     .AsEnumerable()
                     .OrderBy(o => Common.GetPropertyValue(o, orderbyCol))
                     .Take(pindex * psize)
                     .Skip(psize * (pindex - 1)));
            }
            throw new NoSupportCompareTypeException("不支持查询的属性");
        }
Exemplo n.º 12
0
        /// <summary>
        /// 添加一个新的对象,并且该函数会给对象自动赋予UUID,
        /// 如果外部已经有生成一个UUID,执行该函数后UUID会
        /// 被重写,如果数据插入成功,将返回该对象,如果出错,那么
        /// 则返回一个空对象.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static TEntity Add(TEntity e)
        {
            if (Exists(e))
            {
                throw new EntityExistsException("Entity has exists.");
            }
            if (e.UUID == Guid.Empty)
            {
                e.UUID = Guid.NewGuid();
            }
            TreeviewContext context = new TreeviewContext();

            context.GetDatasource <TEntity>().Add(e);
            int flag = context.SaveChanges();

            if (flag > 0)
            {
                return(e);
            }
            return(null);
        }
Exemplo n.º 13
0
 /// <summary>
 /// 检查树节点是否存在子树节点
 /// </summary>
 /// <param name="parentUUID"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 private bool HasChildren(Guid parentUUID, ref TreeviewContext context)
 {
     return(context.TreeNodeSource.Any(m => m.ParentUUID == parentUUID));
 }
Exemplo n.º 14
0
        /// <summary>
        /// 通过UUID查找数据集中的某一个对象
        /// 没找到则返回null
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        public static TEntity Find(Guid uuid)
        {
            TreeviewContext context = new TreeviewContext();

            return(context.GetDatasource <TEntity>().FirstOrDefault(o => o.UUID == uuid));
        }
Exemplo n.º 15
0
        /// <summary>
        /// 获取数据集中的所有元素.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <TEntity> FindAll()
        {
            TreeviewContext context = new TreeviewContext();

            return(context.GetDatasource <TEntity>());
        }
Exemplo n.º 16
0
        /// <summary>
        /// 获取数据仓储中某个类型的对象的总条目数量
        /// </summary>
        /// <returns></returns>
        public static int Count()
        {
            TreeviewContext context = new TreeviewContext();

            return(context.GetDatasource <TEntity>().Count());
        }
Exemplo n.º 17
0
        /// <summary>
        /// 检查数据仓储中是否存在某个类型的对象
        /// </summary>
        /// <returns></returns>
        public static bool HasData()
        {
            TreeviewContext context = new TreeviewContext();

            return(context.GetDatasource <TEntity>().Any());
        }
Exemplo n.º 18
0
        /// <summary>
        /// 查看元素是否已经存在,默认通过UUID
        /// 来判断元素是否有重复
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Exists(TEntity entity)
        {
            TreeviewContext context = new TreeviewContext();

            return(context.GetDatasource <TEntity>().Any(e => e.UUID == entity.UUID));
        }
Exemplo n.º 19
0
        /// <summary>
        /// 通过父级节点的UUID来获取树,顶级节点的UUID为
        /// Guid.Empty
        /// </summary>
        /// <returns></returns>
        public IEnumerable <TreeNode> FindTree(TreeNode parentNode)
        {
            TreeviewContext context = new TreeviewContext();

            return(FindTree(ref context, null));
        }