在关系数据库中存储文件夹层次结构
我有代表文件夹的对象,我想知道它们是否应该在数据库中表示。
一方面,最简单的方法似乎是不表示文件夹对象,而只存储文件夹中包含的对象的路径值。 我看到的问题是,您无法保留其后代不包含任何项目的文件夹,这没什么大不了的。 另外,我不清楚如何加载要显示的文件夹层次结构(例如在 TreeView 中)而不预先将所有内容加载到内存中,这可能是一个性能问题。
另一种方法是使用一个“文件夹”表来引用其父文件夹。 这似乎应该可行,但我不确定如何允许具有相同名称的文件夹,只要它们不共享父级。 这是否应该是数据库应该关心的事情,或者是我应该在业务逻辑中强制执行的事情?
I have objects representing folders and I'm wondering if they should be represented in the database.
On the one hand it seems like the easiest way would be to not represent folder objects and just store a path value for objects contained in a folder. Problems I see with this is that you can't persist an folder whose descendants do not contain any items, which isn't too big of a deal. Also I don't have a clear idea of how to load the folder hierarchy to display (such as in a TreeView) without loading everything into memory upfront, which would probably be a performance issue.
The alternative is to have a "Folder" table with references to its parent folder. This seems like it should work, but I'm unsure how to allow folders with the same name as long as they do not share a parent. Should that even be something the DB should be concerning itself with or is that something that I should just enforce in the the business logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个想法是这样的(自我引用):
The idea is something like this (self-referencing):
查看此页面中间的 ERD。 将层次结构分解到单独的表中允许您支持多种分类法。
Take a look at the ERD in the middle of this page. Factoring out the hierarchy into a separate table permits you to support multiple taxonomies.
SQL Server 有一个支持分层结构的
hierarchyid
数据类型。 请注意,仅适用于完整版本。SQL Server has a
hierarchyid
data type that has support for hierarchical structures. Works only in the full version, mind you.首先问问自己,将层次结构保留在数据库中的目的是什么,以及您可以从中获得什么功能。 然后要求考虑执行此操作所需的工作和维护。
如果您只是使用它来填充树控件,则有一些内置控件直接针对文件夹系统。 这对你来说会更好吗? 通过将其存储在数据库中,您还会获得除此之外的其他东西吗? 您计划如何使数据库与可以在数据库外部更改的实际文件夹系统保持同步? 除非您提供虚拟文件系统,否则最好直接针对数据库中存储的相关路径的真实文件系统。
First ask yourself what the purpose is of keeping the hierarchy in the database and what functionality you gain by that. Then ask consider the work and maintenance that goes into doing it.
If you're simply using it to fill a tree control, there are built-in controls that go directly against the folder system. Would that work better for you? Do you gain something beyond that by storing it in the database? How do you plan to keep the database in sync with the actual folder system, which can be changed outside of the DB? Unless your providing a virtual file system it may be better to just go directly against the real thing with relevant paths stored in the database.