I was going to reply to a feedback to my last post, but from how much I was writing I realized it could deserve a post on its own.
When we decide to adopt the Domain Model pattern to represent data, we're going to deal with complex object graphs: it means that instead of SomeRelatedEntityId properties, we will have concrete references to related entities, a property of SomeRelatedEntity type in this case
Obviously, when you start a business transaction and you know from the beginning that the property X will be accessed, it could be (not always, read below!) a wise choice to eager fetch it to avoid too many DB round trips. However, I consider LazyLoad very useful in at least two practical cases:
- n-level Master-Details: for example, a form with a header to edit the master entity, a grid below for its details and a button for each row which opens an AJAX modal popup to edit the detail's details. Without LazyLoad you should execute a huge query on the beginning (or many smaller ones) to fetch everything, and perhaps the user only wants to modify a Master's field. Oh... and what if the 2nd level details have many-to-one associations towards other entities? How big should our join be?
- There are good chances that the related entity is still in the identity map: let's think about a list of entities with many-to-one association towards the same entity, for example a list of bills of the same customer. Without lazy load, you would fetch the same data many times, with lazy load and identity map, you will have no joins, only one query towards the Customers table, then the same entity will be used for every Bill.