oop - Finder 方法应该是数据映射器的一部分,还是域类的一部分?

标签 oop orm datamapper

在 Martin Fowler 的《企业应用程序架构模式》一书中(德语第 229 页,延迟加载)中,他给出了一个包含以下代码的示例:

public List getProducts() {
    if (products == null) products = Product.findForSupplier(getID());
    return products;
}

如您所见,finder 方法似乎是域类 Product 的一部分。这让我有点困惑,因为我认为与从某处(通常是数据库,但业务逻辑不应该关心)检索对象相关的所有内容都应该是 Data Mapper (PersonDataMapper) 类的一部分。也许我只是错过了什么?

最佳答案

您给出的示例是延迟加载的简单方法。 Person 不太可能使用 DataMapper。正如福勒在英文书 (201) 中所说:

Using lazy initialization is simple, but it does tend to force a dependency between the object and the database. For that reason it works best for ActiveRecord, Table Data Gateway and Row Data Gateway. If you are using Data Mapper, you'll need an additional layer of indirection, which you can obtain by using a virtual proxy [GOF].



至于一切 [...] 应该在 DataMapper 中,嗯...是的,但也不是。
在使用设计模式时,您应该记住什么时候使用它们,什么时候不使用它们。 DataMapper 不是 chalice 。这不是唯一的方法。当您的应用程序只是一个小而简单的 CRUD 应用程序时,数据映射器和/或使用域模型的增加的复杂性可能不值得付出努力。

此外,设计模式是解决常见软件问题的通用良好实践方法。虽然你可以将它们应用到你的具体问题上,因为它们在书中给出,但没有理由虔诚地遵循它们。如果模式中的某些内容会使您的问题解决过于复杂,那么请保持简单。派生。调整。解决这个问题。

关于oop - Finder 方法应该是数据映射器的一部分,还是域类的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1977684/

相关文章:

c++ - 从带有for循环的函数返回时检查对象是否存在

php - Symfony:自定义过滤器/ Hook /事件监听器 - 怎么做?

php - laravel eloquent 中的联合运算符

php - Redbean,多个多对多使用相同的对象

mysql - 如何从两个表中获取数据并按第二个表 Datamapper 排序

具有多个持久层的 PHP DataMapper

javascript - 如何在 Angular 4 中创建组件定义类的实例?

使用死亡钻石进行 C++ 打印

database - 为什么要使用 ORM?

asp.net-mvc - 在 Automapper 中修剪字符串