表模块与域模型[英] Table Module vs. Domain Model

本文是小编为大家收集整理的关于表模块与域模型的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我问选择一种存储用户配置文件的方法前一天,收到 David Thomas Garcia的有趣回应我使用表模块设计模式.看来这可能是我要迈出的方向.我与Google一起出现的一切似乎都是相当高的讨论,因此,如果有人可以将我指向一些例子,或者让我更好地了解所涉及的坚果和螺栓.

推荐答案

最佳参考是马丁·福勒(Martin Fowler)的"企业应用程序架构模式":

这是表模块上的部分摘录:

a 表模块组织域 逻辑,每张表中有一个类 数据库和一个实例 课堂包含各种过程 那将对数据作用.这 与域的主要区别 是,如果您有很多 订单,A 域模型将有一个 每订单订购对象,而a 表 模块将有一个对象要处理 所有订单.

表模块在您描述的用户配置文件数据的灵活数据库架构中特别有用,基本上是 entity-attribute-value 设计.

通常,如果您使用域模型,则基础表中的每一行将成为一个对象实例.由于您将用户配置文件信息存储在多行中,因此您最终必须创建许多域模型对象,而您真正想要的是一个封装所有用户属性的对象.

相反,表模块使您更容易编码适用于基础数据库表中多个行的逻辑.如果您为给定用户创建一个配置文件,则指定所有这些属性,并且表模块类将具有将其转换为一系列INSERT语句的代码,每个属性一行.

$table->setUserProfile( $userid, array('firstname'=>'Kevin', 'lastname'=>'Loney') );

同样,查询给定用户的配置文件将使用表模块映射查询结果的多行设置为对象成员.

$hashArray = $table->getUserProfile( $userid );

本文地址:https://www.itbaoku.cn/post/597593.html

问题描述

I asked about Choosing a method to store user profiles the other day and received an interesting response from David Thomas Garcia suggesting I use the Table Module design pattern. It looks like this is probably the direction I want to take. Everything I've turned up with Google seems to be fairly high level discussion, so if anyone could point me in the direction of some examples or give me a better idea of the nuts and bolts involved that would be awesome.

推荐答案

The best reference is "Patterns of Enterprise Application Architecture" by Martin Fowler:

Here's an excerpt from the section on Table Module:

A Table Module organizes domain logic with one class per table in the database, and a single instance of a class contains the various procedures that will act on the data. The primary distinction with Domain Model is that, if you have many orders, a Domain Model will have one order object per order while a Table Module will have one object to handle all orders.

Table Module would be particularly useful in the flexible database architecture you have described for your user profile data, basically the Entity-Attribute-Value design.

Typically, if you use Domain Model, each row in the underlying table becomes one object instance. Since you are storing user profile information in multiple rows, then you end up having to create many Domain Model objects, whereas what you really want is one object that encapsulates all the user properties.

Instead, the Table Module makes it easier for you to code logic that applies to multiple rows in the underlying database table. If you create a profile for a given user, you'd specify all those properties, and the Table Module class would have the code to translate that into a series of INSERT statements, one row per property.

$table->setUserProfile( $userid, array('firstname'=>'Kevin', 'lastname'=>'Loney') );

Likewise, querying a given user's profile would use the Table Module to map the multiple rows of the query result set to object members.

$hashArray = $table->getUserProfile( $userid );