最佳实践 - 元数据和功能定义数据是否应该混合?

发布于 2024-07-06 10:16:52 字数 286 浏览 7 评论 0原文

考虑一个简单的新闻文章 Web 应用程序的情况,该应用程序的数据库表列为“状态”,可通过单选按钮集访问该列:

状态 - [x] 发布 [ ] 草稿 [ ] 存档

... 其中“发布”显示公开的文章,而“草稿”和“存档”则不然。 从功能上讲,“草稿”和“存档”做同样的事情,但带有额外的元数据含义。 “显示”和“隐藏”两种功能状态以及“发布”、“草稿”和“存档”元数据混合在“状态”的同一列中。

这是一个好的做法吗? 虽然这是一个非常简单的案例,但较大的案例可能会暴露这种做法的缺陷(或不……)。

Consider the case of a simple news article web application that has a DB table column of "Status" that is accessible by a radio button set of:

Status - [x] Publish [ ] Draft [ ] Archive

...where "Publish" shows an article publicly and "Draft" and "Archive" do not. Functionally "Draft" and "Archive" do the same thing but carry additional meta data meanings. The two functional states of "show" and "hide" along with the meta data of "publish", "draft" and "archive" are intermixed in the same column of "status".

Is this a good practice? While this is a very simple case, larger cases might reveal flaws with such a practice (or not...).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-07-13 10:16:52

功能状态与行为有关 - 它们不需要在数据库中建模。 如果您的业务逻辑只关心“显示”状态为“已发布”的文章,则没有理由使用“显示”列使数据的复杂性加倍。

当您确定业务逻辑需要额外数据来决定是否显示或隐藏文章(可能是 IsApproved 标志)时,您可以存储该数据。

换个角度看,如果再添​​加一栏“显示”,那么状态为“草稿”且“显示”= 1 的文章会做什么呢? 根据你的业务规则,那是无效状态。

Functional states are about behavior - they do not need to be modeled in your database. If your business logic only cares about "showing" articles with a status of "Published" - there's no reason to double the complexity of your data with a Show column.

At the point that you decide your business logic needs additional data to make the decision whether to show or hide an article (perhaps an IsApproved flag), then you can store that data.

Looking at it from a different angle - if you were to add another column of "Show", then what would an article with a status of "Draft" and "Show" = 1 do? According to your business rules, that is an invalid state.

薄暮涼年 2024-07-13 10:16:52

在这种情况下,我想说这是合适的功能。

我们都在媒体上见过 WTF,有人不小心点击了 show[x]
同时草稿[x]。

现在这样,不可能一不小心就露出草稿了。 这在报纸上很重要,因为记者因这样的事情而臭名昭著:

StackOverflow 的 John Doe 说,“---我不记得那个丑陋的混蛋说了什么 - 检查磁带并稍后填写”

这可能不应该被打印。

In this instance, I would say that this is the appropriate functionality.

We've all seen WTF's in the media where someone accidentally hit show[x]
and draft[x] at the same time.

The way it is now, it is impossible to accidentally show a draft. This is important in newspapers as reporters are notorious for stuff like:

John Doe, of StackOverflow said, "---I can't remember what that ugly f*cker said - Check the tape and fill in later"

Which probably shouldn't be printed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文