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

查看:247
本文介绍了表模块与域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问过选择存储用户个人资料的方法其他日子,并收到来自David Thomas Garcia的有趣反应我使用表模块设计模式。看起来这可能是我想要的方向。我用谷歌翻译的所有东西,似乎都是相当高层次的讨论,所以如果有人可以指出我的方向的一些例子或让我更好地了解所涉及的螺母和螺栓是真棒。

$最好的参考是Martin Fowler的企业应用程序架构的模式:



下面是表格模块部分的摘录:


表格模块组织域

数据库中的每个表一个类,以及
类的单个实例包含将作用于数据的各种过程
。 < em> Domain
Model 的
主要区别在于,如果您有许多
订单,则域模型将有一个
每个订单的订单对象,而表
模块将有一个对象处理
所有订单。


表模块在您为用户配置文件数据描述的灵活数据库体系结构中特别有用,基本上是 Entity-Attribute-Value 设计。



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



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

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

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

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


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 );

这篇关于表模块与域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆