I have an object model that looks like this (pseudo code):
class Product {
public ISet Recommendations {get; set;}
public ISet
If all you want is avoiding the N+1 trouble, use Batch fetching of lazy loads instead of eager loading.
It removes N+1 issues while having a minimal impact on code: you just have to change a configuration parameter or adjust the mappings.
In configuration, set default_batch_fetch_size
to some sensible value for your usual lazy-loads counts. 20
is usually a good value.
Or in mappings, set the batch-size
attributes on classes (
) and collections (
,
, ...) for controlling case by case the lazy-load batching.
This will configure your lazily loaded entities and collections of entities to not only load themselves, but also some others awaiting entities (of the same class) or collections of entities (same collections of other entities of the same class).
I have written a detailed explanation of it in this other answer.