nhibernate-core
nhibernate-core copied to clipboard
NHibernateUtil.Initialize with multiple proxy objects using future queries
Using future queries is a nice easy way to improve data access performance.
It would be great if there was an overload for NHibernateUtil.Initialize()
that accepted multiple proxy objects at once which would then load the data as future queries using one round trip to the db.
Batch fetching already does that. Initializing one proxy will also initialize up to batch-size - 1
other uninitialized proxies of the same class bound to the same session.
So, it is does not allow to control which proxies will be initialized, and it does not allow to initialize in the same round-trip proxies of different classes. But it works with very little code: just adjust the mapping or the global configuration. Then it will works everywhere a proxy is initialized, explicitly (with an Initialize
call) or implicitly (by accessing its unloaded properties), without the need of additional code.
If an application needs a more controlled way of specifying what to initialize at the same time, you may still write future queries loading the entities matching the desired proxies with a in
on their identifiers for those of the same class. It is little less straightforward than an Initialize
taking multiple proxies, but still quite lightweight to code, excepted for entities using composite ids.
Still yes, it could be nice to have the option directly. Special care would have to be given about how it should interact with batch fetching, though. It would likely have to disable it as soon as more than one proxy is provided to Initialize
, which could be seen as an unexpected behavior discrepancy.
Sorry, I did not explain my self very well. I was talking about initializing different proxy objects e.g.
public class MyEntity {
...
public ISet<Foo> FooChildren {get; set; }
public ISet<Bar> BarChildren {get; set; }
public OtherEntity SomeReference {get; set;}
}
If you have an operation that will access all those lazy loaded properties then you will get multiple queries to populate them. I was wanting to be able to initialize them all at once using future queries to reduce the round trips. Something like:
NHibernateUtil.Initialize(myentity.FooChildren, myentity.BarChildren, myentity.SomeReference);
Agreed, It would be great to have initialization with multiple proxy objects.
'cause I have entities that can have ~20 nested objects, via specific key I need to initialize half of them and with another key initialize 2nd half. And when I do it to each object I have multiple calls to DB