基于数据映射器的多态域对象

发布于 2024-07-12 00:21:37 字数 385 浏览 4 评论 0原文

我有一个基本的域对象,例如由数据库中的单个表表示的人员、活动或事件。 但是,我还有这些对象的更复杂版本,例如 PersonCampaign 或 PersonEvent 甚至 CampaignEvent,理论上可以扩展基础对象之一。

然而,由于多种原因,这变得很复杂,因为 PHP 不支持多重继承(例如 PersonEvent 扩展 Person 或 Event)。 还因为某些域对象实际上是具有不同属性和功能的工厂对象(例如,事件实际上是按事件类型(如电子邮件、呼叫、传真)进行子类化的)。

我能看到的最简单的解决方案是根据从数据访问层返回的数据来更改对象的实际性质。

有人对处理这个问题有更好的建议吗? 或者创建可以根据数据访问层当前可用的内容来更改属性和行为的统一域对象是否正确?

I have a basic domain object, say like Person or Campaign or Event that is represented by a single table in the database. However, I also have more complicated versions of these objects say like a PersonCampaign or PersonEvent or even CampaignEvent that could theoretically extend one of the base objects.

However, for a number of reasons this becomes complicated because PHP does not support multiple inheritance (for example does PersonEvent extend Person or Event). And also because some domain objects are actually factory objects with varying properties and functionality (for example Event is actually subclassed by the type of event like email, call, fax).

The easiest solution that I can see is to have the actual nature of the object change based on what data is returned from the data access layer.

Does anyone have any suggestions on a better way to handle this? Or is it correct to create unified domain objects that can change properties and behavior based on what is currently available to it from the data access layer?

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

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

发布评论

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

评论(3

荒路情人 2024-07-19 00:21:37

解决方案是封装其他对象:

PersonEvent 类包含一个 Person 和一个 Event。

可通过函数 $PersonEvent->get_event() 或属性 $PersonEvent->Event; 访问

A solution is the encapsulate the other objects:

The PersonEvent class contains a Person and an Event.

Accessable by either a function $PersonEvent->get_event() or property $PersonEvent->Event;

穿越时光隧道 2024-07-19 00:21:37

从 OOP 的角度来看,PersonEvent 并不是真正的对象,而是一种关系。

Person 类可以获得如下函数:

get_events()
add_event($Event)
remove_event($Event)

和 Event 类

get_person()
set_person($Person)
unset_person() // set person_id to NULL

(假设人和事件之间存在 1:N 关系)

不好的部分是,这将使数据映射器变得复杂或根本不会使用数据映射器。

From a OOP perspective a PersonEvent isn't really a object, its a relation.

The Person class could get functions like:

get_events()
add_event($Event)
remove_event($Event)

and the Event class

get_person()
set_person($Person)
unset_person() // set person_id to NULL

(Assuming a 1:N relation between person and event)

The bad part is, this will complicate the data-mapper or wont use the data-mapper at all.

无声情话 2024-07-19 00:21:37

设置如下:

表:人员、销售人员、客户人员等怎么样?
人员表存储有关人员的所有子类型的通用信息。 然后还有其他表 FK 到 person 表。 它存储有关该子类型的附加独特信息。

您的对象将扩展或调用通用“人”对象来访问主要数据,然后调用它的唯一类型对象来访问该子类型的唯一数据?

What about having a setup like:

Table: Person , Sales Person, Customer Person, etc..
The person table stores the generic information about all sub-types of persons. Then there are other tables FK'd to the person table. It stores additional unique information about that sub-type.

Your objects would either extend, or call the generic 'person' object to access primary data, then call it's unique type object to get access the unique data for that sub-type?

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