Understanding Named Aggregates
Associations and fields have different default behaviours, and it is possible to get confused about how aggregates and the WithAggregate operator affect the loading of fields. This section summarises load behaviour so you can understand exactly what gets loaded and when under different circumstances.
· If you don’t do anything to a field, it will always be loaded when the entity loads.
· If you specify one or more aggregate names on a field, it will be loaded only when it is accessed, incurring a separate database access, unless you specify one of those aggregates in your query.
· If you don’t do anything to an association, it will be loaded only on demand, incurring a separate database access.
· If you specify eager load on an association, it will always be loaded when the entity loads, as part of the same database access.
· If you specify one or more aggregate names on an association (and don’t specify eager load), it will be loaded only on demand, unless you specify one of those aggregates in your query, in which case it will be loaded when the entity loads, like an eager load association.
· WithAggregate doesn’t affect fields or associations that aren’t part of a named aggregate. They will stay eager or lazy depending on how they were set up.
· WithAggregate propagates across associations, so you can use a named aggregate to load an entire object graph.
· You can specify multiple named aggregates on a field or association to control which subsets of entity data and/or the object graph get loaded in different situations.
You can use the designer to help you visualise named aggregates. See Custom Views in the chapter Working with Models in the Visual Designer.