【问题标题】:Generating a directory folder structure from database从数据库生成目录文件夹结构
【发布时间】:2020-08-23 16:55:36
【问题描述】:

发现自己陷入了循环。我有一个数据库表,它定义了一个目录文件夹结构,其中可能包含无限数量的子文件夹。

最终结果文件夹结构应如下所示,但逻辑应允许更改此结构要求:

鉴于上述文件夹结构的数据:

最重要的字段是idpidpid of NULL 代表顶级文件夹(电子邮件、TM 应用程序、TM 争议)。所有其他文件夹都是子文件夹,它们分为 3 个级别,存储在 level_count 字段中。不确定我是否真的需要 level_count 字段。我一直在努力使逻辑尽可能“灵活”。 pid 定义文件夹的直接父级:

我目前的解决方案不够好,因为它不能处理 "infinite" 的级别数,它只支持三个级别。我宁愿不必知道关卡的数量。

如果可能,我希望能够保留核心逻辑,并且我不想以先创建所有父文件夹,然后再返回创建子文件夹的方式来更改它。相反,我想深入到最深处,创建这些文件夹,然后返回给父母。我认为这段代码代表了这个想法,如果我说得通的话。

foreach (DataRow r in dtParentFolders.Rows) // these are the 3 parent rows with null pid
{                
    int parentFolderId = Convert.ToInt32(r["id"]);
    string parentFolderName = r["folder_name"].ToString();

    //Create folder
    Console.WriteLine(parentFolderName);

    DataTable dt = GetFolders(parentFolderId);

    foreach (DataRow r2 in dt.Rows)
    {
        parentFolderId = Convert.ToInt32(r2["id"]);
        CreateFolder(r2);
        dt = GetFolders(parentFolderId);

        foreach (DataRow r3 in dt.Rows)
        {
            parentFolderId = Convert.ToInt32(r3["id"]);
            CreateFolder(r3);
            dt = GetFolders(parentFolderId);
        }
    }
}

【问题讨论】:

  • 那么这里的问题是如何创建CRUD程序来维护你的目录表?
  • 您是否考虑过标签结构,其中一个项目位于多个类别中?可能会改善体验
  • @SteveC 不,我问的是如何重写逻辑,以便它可以处理 x 个不同的文件夹/子文件夹结构。数字子文件夹级别可以更改。我的逻辑只下降了 3 个级别。

标签: c# database-design logic


【解决方案1】:

我希望这对您有所帮助。

    public class Record
    {
        public int Id { get; set; }

        public int PId { get; set; }

        public string Name { get; set; }
    }

    

    public static void Main()
    {
        var records = new List<Record>() 
        { 
            new Record { Id = 1,    Name = "MainDir1",      PId = 0 },
            new Record { Id = 2,    Name = "MainDir2",      PId = 0 },
            new Record { Id = 3,    Name = "MainDir3",      PId = 0 },
            new Record { Id = 4,    Name = "SubDir1",       PId = 1 },
            new Record { Id = 5,    Name = "SubDir2",       PId = 2 },
            new Record { Id = 6,    Name = "SubSubDir1",    PId = 5 },
            new Record { Id = 7,    Name = "SubSubDir2",    PId = 5 },
            new Record { Id = 8,    Name = "SubSubDir3",    PId = 5 },
            new Record { Id = 9,    Name = "SubSubDir4",    PId = 5 },
            new Record { Id = 10,   Name = "SubSubDir5",    PId = 5 },
        };

        var node = new Directory(0, null, null);

        records
            .OrderBy(x => x.PId)
            .ThenBy(x => x.Id)
            .ThenBy(x => x.Name)
            .ToList()
            .ForEach(x => node.AddChild(x.Name, x.Id, x.PId));

        node.Print();
    }



    public class Directory
    {
        public Directory(int id, string name, Directory parent)
        {
            this.Id = id;
            this.Name = name;
            this.Parent = parent;
            this.Indentation = parent is null ? 0 : parent.Indentation + 1;

            this.Children = new HashSet<Directory>();
        }

        public int Id { get; set; }

        public int Indentation { get; set; }

        public string Name { get; set; }

        public Directory Parent { get; set; }

        public ICollection<Directory> Children { get; set; }

        public void AddChild (string name, int id, int parentId)
        {
            if (this.Id == parentId)
            {
                this.Children.Add(new Directory(id, name, this));
                return;
            }

            foreach (var child in this.Children)
            {
                child.AddChild(name, id, parentId);
            }
        }

        public void Print()
        {
            Console.WriteLine($"{new string(' ', this.Indentation * 4)}{this.Name}");

            foreach (var child in this.Children)
            {
                child.Print();
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 2018-07-06
    • 2012-10-01
    • 2016-05-05
    • 2023-03-19
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多