为什么活动记录模式不适用于丰富的域模型?

发布于 2024-10-31 18:11:55 字数 305 浏览 0 评论 0原文

我正在阅读 POEAA 的架构模式章节,Fowler 说“随着领域逻辑变得更加复杂,并且您开始走向丰富的领域模型 (116),Active Record (160) 的简单方法开始崩溃当您将域逻辑分解为较小的类时,域类与表的一对一匹配开始失败。关系数据库不处理继承,因此使用策略 [Gang of Four] 和其他简洁的 OO 模式变得很困难。随着领域逻辑变得活跃,您希望能够测试它而不必一直与数据库对话。”

我真的不明白这一点。通过“域类与表的一对一匹配”,他是否意味着仅适用于没有关联或单表继承层次结构的类?

为什么将领域逻辑分解为更小的类会导致该模式失败?

I'm reading the architectural patterns chapter of POEAA, and Fowler says that "As the domain logic gets more complicated and you begin moving toward a rich Domain Model (116), the simple approach of an Active Record (160) starts to break down. The one-to-one match of domain classes to tables starts to fail as you factor domain logic into smaller classes. Relational databases don't handle inheritance, so it becomes difficult to use strategies [Gang of Four] and other neat OO patterns. As the domain logic gets feisty, you want to be able to test it without having to talk to the database all the time."

I didn't really understand this. By "one to one match of domain classes to tables", does he mean that only for classes where there is no associations or single table inheritance hierarchy?

And why does factoring domain logic into smaller classes cause the pattern to fail?

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

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

发布评论

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

评论(2

忆沫 2024-11-07 18:11:55

他想说的是,更复杂的领域模型通常不仅仅是“表中的数据”。福勒所说的这些更复杂的模型是从不同的表、视图甚至其他来源获取数据的模型。

Active Record 模式不太适合此目的,而仅与模型类(仅包含业务逻辑并且不与数据访问层通信)相结合的 DataMapper 模式可能更适合此类情况。

Active Record 模式在这里失败了,因为它或多或少直接映射到数据库中的表。

我不知道确切的模式定义,所以如果我错了,请纠正我。

What he's trying to say is that more complex domain models are usually more than just "data from a table". These more complex models Fowler is talking about are models that get data from different tables, views or maybe even other sources.

The Active Record pattern is not very suitable for this purpose, and the DataMapper pattern combined with just model classes (containing just the business logic and do not communicate with the data access layer) is probably more suitable in situations like these.

The Active Record pattern fails here, because it maps more or less directly to a table in the database.

I don't know the exact pattern definition, so please correct me if i'm wrong.

各自安好 2024-11-07 18:11:55

不,我认为他在谈论领域逻辑。通过活动记录,对象同时携带数据和行为。所以这是一场一对一的比赛。如果您开始分离数据/行为,就像在数据映射器模式中一样,它就会变成一对多。我的印象是,有时你真的必须像读学术废话一样读那本书才能理解他的意思。 :-)

No, I think he is talking about the domain logic. With active record the object carries both data and behavior. So that is a one-to-one match. If you start separating data/behaviour, like in the Data Mapper pattern, it becomes one-to-many. I have the impression that you sometimes really have to read that book like academic nonsense to understand what he means. :-)

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