【发布时间】:2009-07-18 17:36:44
【问题描述】:
查看 DDD,我们将数据库抽象为我们操作的各种模型,并将其视为模型所在的存储库。然后我们在其上添加数据层和服务/业务层。我的问题是,这样做是否会通过构建胖模型导致数据传输效率低下?
例如,假设我们的系统在屏幕上显示客户的发票。 从 OOP 的角度考虑它,我们最终可能会得到一个看起来有点像这样的对象:
class Invoice {
Customer _customer;
OrderItems _orderitems;
ShippingInfo _shippingInfo;
}
class Customer {
string name;
int customerID;
Address customerAddress;
AccountingInfo accountingInfo;
ShoppingHistory customerHistory;
}
(for the sake of the question/argument,
let's say it was determined that the customer class had to
implement AccountingInfo and ShoppingHistory)
如果发票单独需要打印客户姓名,我们为什么要随身携带所有其他行李?使用存储库类型的方法似乎我们将构建这些需要所有这些资源(CPU、内存、复杂查询连接等)的复杂域对象,然后通过管道将其传输到客户端。
简单地将 customerName 属性添加到发票类将脱离抽象,并且看起来是一种可怕的做法。另一方面,半填充像 Customer 这样的对象似乎是一个非常糟糕的主意,因为您最终可能会创建同一个对象的多个版本(例如,一个有地址但没有 ShoppingHistory,一个有 AccountingInfo 但没有地址等)。我错过了什么,或者不明白什么?
【问题讨论】:
标签: performance repository design-patterns data-transfer