NHibernate lazy loading nested collections with futures to avoid N+1 problem

后端 未结 3 1467
轮回少年
轮回少年 2020-12-14 04:07

I have an object model that looks like this (pseudo code):

class Product {
    public ISet Recommendations {get; set;}
    public ISet

        
3条回答
  •  天涯浪人
    2020-12-14 04:21

    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.

提交回复
热议问题