模型的粒度化?

发布于 2024-08-18 05:15:03 字数 1527 浏览 6 评论 0原文

我正在开发一个主要基于 Zend Framework 组件的 CMS。该 CMS 的一些数据库表如下:

site
| id | name |
-------------

locale
| languageCode | regionCode |
-----------------------------

site_locale // link sites with locales
| siteId | languageCode | regionCode | isActive | isDefault |
-------------------------------------------------------------

我有一个名为 Site 的模型,其中包括以下方法:

getId()
getName()
listLocales() // list all locales for this site

我对于应该定义的粒度如何持观望态度模型:

一种选择是从 listLocales() 方法返回 SiteLocale 对象/模型(换句话说,数据库表表示),其中这些 SiteLocale code> 对象包含以下方法:

getSite() // returns the Site model
getLocale() // returns a Zend_Locale
isActive() // is this locale active for the site this model represents?
isDefault() // is this the default locale for the site this model represents()

另一种选择是简单地在 Site 模型中创建以下方法,然后使用它来完成:

getDefaultLocale() // simply return the default site locale as Zend_Locale
listActiveLocales() // simply return all active site locales as Zend_Locales
listAllLocales() // simply return all site locales as Zend_Locales

您认为正确的方法是什么?为什么?

此外,第一个选项(或者甚至两个选项)是否会违反德米特法则

编辑(1 月 22 日)
尽管我喜欢杰夫的答案,但我仍然对新的/其他观点持开放态度。

I'm developing a CMS largely based on Zend Framework components. Some of the database tables for this CMS are as followed:

site
| id | name |
-------------

locale
| languageCode | regionCode |
-----------------------------

site_locale // link sites with locales
| siteId | languageCode | regionCode | isActive | isDefault |
-------------------------------------------------------------

I have a model named Site which consists, amongst others, of the following methods:

getId()
getName()
listLocales() // list all locales for this site

I'm kind of on the fence on how granularized I should define models:

One option would be to return SiteLocale objects/models (in other words a DB table representation) from the listLocales() method, where these SiteLocale objects contain the following methods:

getSite() // returns the Site model
getLocale() // returns a Zend_Locale
isActive() // is this locale active for the site this model represents?
isDefault() // is this the default locale for the site this model represents()

The other option would be to simply create the following methods in the Site model, and be done with it:

getDefaultLocale() // simply return the default site locale as Zend_Locale
listActiveLocales() // simply return all active site locales as Zend_Locales
listAllLocales() // simply return all site locales as Zend_Locales

What do you feel is the right way to go? And why?

Furthermore, would the first option (or perhaps even both options) violate the Law of Demeter?

EDIT (22 jan)
Although I like Jeff's answer, Im still open for new/other perspectives.

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

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

发布评论

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

评论(1

独﹏钓一江月 2024-08-25 05:15:03

首先,关于数据库表:您可能可以进一步规范化数据库。 locale 和 site_locale 表之间存在重复。当然,我在这里没有看到大局,所以你的做法背后可能有一些东西。

坦率地说,任何一个选择都很好。我会选择使您的代码更具可读性和可维护性的设计。例如,如果您选择第一个选项,您最终会得到这样的循环吗?

site_locales = site.listLocales()
foreach (site_locale in site_locales) {
    if site_locale.isDefault() {
        do_something(site_locale.getLocale())
    }
}

如果是这样,那么我会避免它并选择第二个选项,最终得到:

do_something(site.getDefaultLocale())

快速浏览一下就更容易理解。也许它甚至会提高您网站的性能。

但是,如果您认为将来要做的大量工作都需要使用 SiteLocales 列表,但您并不确切知道除了 getDefaultLocale() 之外还要做什么, listActiveLocales()listAllLocales(),那么也许第一个选项可能是理想的。或者您甚至可以结合使用两者。

至于德墨忒尔法则,它更像是德墨忒尔准则。打破任何规则都是可以的,只要你有意识地去做,理解为什么这样做,并理解后果(如果有的话)。例如,如果违反法律会导致代码更易于维护和可读,但您仍然在应用程序中保留高级别的关注点分离,那么通常是可以的。所以我不担心这两种选择是否违法。

First, regarding the database tables: You could probably normalize the database further. There's duplication between the locale and site_locale tables. Of course, I'm not seeing the big picture here so there might be something behind the way you've done it.

Frankly, either option is fine. I would choose the design that makes your code more readable and maintainable. For instance, if you chose the first option, would you end up with loops like this all over the place?

site_locales = site.listLocales()
foreach (site_locale in site_locales) {
    if site_locale.isDefault() {
        do_something(site_locale.getLocale())
    }
}

If so, then I'd avoid it and go with the second option and end up with:

do_something(site.getDefaultLocale())

This is much more understandable with a quick glance. Maybe it'd even improve your site's performance.

However, if you think you're going to do a lot of work that utilizes lists of SiteLocales in the future, but you don't know exactly what you're going to do beyond getDefaultLocale(), listActiveLocales(), and listAllLocales(), then perhaps the first option might be ideal. Or you could even use a combination of the two.

As for the Law of Demeter, it's more like the Guideline of Demeter. It's OK to break any rule as long as you do it consciously, understand why you're doing it, and understand the consequences, if any. For instance, if breaking the law leads to more maintainable and readable code, yet you still preserve a high-level separation of concerns in your application, it's generally OK. So I wouldn't worry about whether or not either option breaks the law.

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