Domain Objects containing lots of Data

Our Domain has a need to deal with large amounts (possibly more than 1000 records worth) of objects as domain concepts. This is largely historical data that Domain business logic needs do use. Normally this kind of processing depends on a Stored Procedure or some other service to do this kind of work, but since it is all intimately Domain Related, and we want to maintain the validity of the Model, we'd like to find a solution that allows the Aggregate to manage all of the business logic and rules required to work with the data.

Essentially, we're talking about past transaction data. Our idea was to build a lightweight class and create an instance for each transaction we need to work with from the database. We're uncomfortable with this because of the volume of objects we'd be instantiating and the potential performance hit, but we're equally uncomfortable with offloading this Domain logic to a stored procedure since that would break the consistency of our Model.

Any ideas on how we can approach this?

13.10.2009 17:12:04
4 ОТВЕТА

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.

0
23.05.2017 12:19:37
Is Lazy loading still an option if we know we need all of those records at once to do searching, sorting, summing etc? I thought lazy loading was mainly for when you have data that you may not need to load until a certain time and you want to spread out the work.
jlembke 13.10.2009 18:04:09
No, you're absolutely right - it's not useful in those scenarios. For aggregate operations I think it's important to make pragmatic compromises and move some logic outside the domain model.
Jeff Sternal 13.10.2009 18:08:20
In retrospect, the key words in my post are "potential performance hit." As I think about this, I may be engaging in "premature optimization." Maybe I should find out if this is actually a problem first.. :)
jlembke 13.10.2009 18:44:48
Nevertheless, I believe the intent of the question deserves to be considered. While it may very well be premature optimization this time, there are certainly cases where behavior requiring inordinate amounts of data fall clearly within the responsibility of a given domain concept.
Derek Greer 19.10.2009 15:33:22

"1000" isn't really that big a number when it comes to simple objects. I know that a given thread in the system I work on may be holding on to tens of thousands of domain objects at a given time, all while other threads are doing the same at the same time. By the time you consider all of the different things going on in a reasonably complicated application, 1000 objects is kind of a drop in the bucket.

YMMV depending on what sort of resources those objects are holding on to, system load, hard performance requirements, or any number of other factors, but if, as you say, they're just "lightweight" objects, I'd make sure you actually have a performance problem on your hands before you try getting too fancy.

1
15.10.2009 20:00:21

When you say 1000 records worth, do you mean 1000 tables or 1000 rows? How much data would be loaded into memory?

0
15.10.2009 20:01:35

It all depends on the memory footprint of your objects. Lazy loading can indeed help, if the objects in question references other objects which are not of interest in your process.

If you end up with a performance hog, you must ask yourself (or perhaps your client) if the process must run synchronously, or if it can be offloaded to a batch process somewhere else.

Using DDD, How Does One Implement Batch Processing?

0
23.05.2017 12:13:36