I am missing an efficient way in NHibernate to fetch several collections.
Say that you have a list of entities already in NHibernate session and want
to fetch two collections for these. You could do something like this.
Session.QueryOver<MyEntity>()
.Fetch(p => p.Collection1).Eager
.WhereRestrictionOn(e => e.Id).IsInG(ids)
.List();
Session.QueryOver<MyEntity>()
.Fetch(p => p.Collection2).Eager
.WhereRestrictionOn(e => e.Id).IsInG(ids)
.List();
However this is fetching much more data than is needed, joining over the
main table several times.
I have experimented with a strategy to only fetch the data that is
necessary (see
code<https://github.com/tobias-eriksson-stratsys/NHibernateExperiments/blob/master/Infrastructure/CollectionFetcher.cs>
)
Then you could do like this.
var fetcher = new CollectionFetcher(Session);
fetcher.Fetch(entities, p => p.Collection1);
fetcher.Fetch(entities, p => p.Collection2);
Now I want to take it one step further and batch everything in one query
something like this
var fetcher = new CollectionFetcher(Session);
fetcher
.Fetch(entities, p => p.Collection1)
.Fetch(entities, p => p.Collection2)
.Execute()
I have looked at how MultiQuery handles this and it seems quite easy to do,
but the methods on the Loader class is internal so I can’t access these
from outside of NHibernate.
Does anyone have any ideas how I can take this idea further?