6

During my short experience with DDD I often find myself in the situation where I wished for some type of lazy loading mechanism to solve some dangerous cases where large nested data sets might be loaded.

However after a while I realized that each time, that was the symptom of a poorly designed aggregate root that was taking too much responsibility. Additionally it used to be the case where the aggregates should be fully loaded to perform consistency operations. Getting used to work with Entity ids made things slightly easier.

So after a while, I am wondering... is lazy loading a code smell? Is it something just to build non mature models without causing a big problem?

2
  • You should read Vaughn Vernon's essay on Effective Aggregate Design: vaughnvernon.co/?p=139
    – JefClaes
    Dec 29 '13 at 16:26
  • I just found this question after trying to search YouTube's website's apparent lazy-loading of Watch History deletion. Searched lazy loading antipattern on Google (lol). Anyway, I just wanted to let you know that you asked an excellent question that I don't think I've ever considered. I guess I'm 7 years and 4 months late to the game hahaha. Cheers, anyway! EDIT: Wow! This webpage literally just updated itself to say "7 years, 3 months ago" as soon as I saved my comment! Shit you not. Makes me wonder how much lazy-loading is being done between Chrome, Google (Alphabet), and Stack Exchange. May 9 at 19:00
9

My short answer: yes!

Lazy loading and other fetching strategies indicate that the domain model is being used to perform queries. This is something that should not be done. A simple query layer best serves that purpose. So, in my opinion, you have come to the correct conclusion :)

The domain model should only contain current transactional state. This means that, typically, there should be no large data sets as these are more common for historical data. This does not mean that historical data cannot be used to create new transaction aggregates, though.

1
  • 1
    Just a few days later and I have run into a situation where I am so relieved I forced myself to reference entities just by ID and avoid hiding a query with lazy loading. While lazy-loading stops the load of a huge nested data set into memory, it makes easier running into the problem of serializing an Entity into a huge document (like JSON for a web interface) and having to 'rethink' one extra model to export my data outside my Domain layer. Jan 4 '14 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.