4

我们的领域需要处理大量(可能超过 1000 条记录)作为领域概念的对象。这主要是领域业务逻辑需要使用的历史数据。通常这种处理依赖于存储过程或其他一些服务来完成这种工作,但是由于它都是与领域密切相关的,并且我们想要保持模型的有效性,我们希望找到一个解决方案允许聚合管理处理数据所需的所有业务逻辑和规则。

本质上,我们谈论的是过去的交易数据。我们的想法是构建一个轻量级的类,并为我们需要从数据库处理的每个事务创建一个实例。由于我们要实例化的对象的数量和潜在的性能损失,我们对此感到不舒服,但我们同样不喜欢将此域逻辑卸载到存储过程,因为这会破坏我们模型的一致性。

关于我们如何解决这个问题的任何想法?

4

4 回答 4

1

对于简单的对象,“1000”并不是一个很大的数字。我知道我工作的系统中的给定线程可能在给定时间持有数以万计的域对象,而其他线程同时也在做同样的事情。当您考虑在一个相当复杂的应用程序中发生的所有不同事情时,1000 个对象只是杯水车薪。

YMMV 取决于这些对象所持有的资源类型、系统负载、硬性能要求或任何其他因素,但如果如你所说,它们只是“轻量级”对象,我会确保你在你尝试太花哨之前,实际上你手上有一个性能问题。

于 2009-10-15T19:55:07.473 回答
0

当您说价值 1000 条记录时,您是指 1000 个表还是 1000 行?有多少数据会加载到内存中?

于 2009-10-15T20:01:35.790 回答
0

这完全取决于对象的内存占用。如果有问题的对象引用了您的过程中不感兴趣的其他对象,则延迟加载确实会有所帮助。

如果您最终遇到性能问题,您必须问自己(或者您的客户)该进程是否必须同步运行,或者是否可以将其卸载到其他地方的批处理进程。

使用 DDD,如何实现批处理?

于 2009-10-15T21:19:39.783 回答
0

Lazy loading is one technique for mitigating this problem and most of the popular object-relational management solutions implement it. It has detractors (for example, see this answer to Lazy loading - what’s the best approach?), but others consider lazy loading indispensable.

Pros

  • Can reduce the memory footprint of your aggregates to a manageable level.
  • Lets your ORM infrastructure manage your units of work for you.
  • In cases where you don't need a lot of child data, it can be faster than fully materializing ("hydrating") your aggregate root.

Cons

  • Chattier that materializing your aggregates all at once. You make a lot of small trips to the database.
  • Usually requires architectural changes to your domain entity classes, which can compromise your own design. (For example, NHibernate just requires you to expose a default constructor make your entities virtual to take advantage of lazy loading - but I've seen other solutions that are much more intrusive).

By contrast, another approach would be to create multiple classes to represent each entity. These classes would essentially be partial aggregates tailored to specific use cases. The main drawback to this is that you risk inflating the number of classes and the amount of logic that your domain clients need to deal with.

于 2009-10-13T17:52:19.210 回答