c# - load child objects after session closed -
i want load objects sub after session closed, anyway that?
using (var session = nhibernatehelper<t>.opensession()) { foo = session.createcriteria(foo).list<foo>(); } using (var session = nhibernatehelper<t>.opensession()) { foo.children.load(); } public static void load<t>(this ilist<t> list) { //load foo children }
we can here profit nhibernate native support working detached objects
19.1.4. initializing collections , proxies
...
may attach loaded object new isessionmerge()orlock()before accessing uninitialized collections (or other proxies). no, nhibernate not, , should not automatically, since introduce ad hoc transaction semantics!
...
the adjusted load() method this:
public static tentity loadcollection<tentity, titem>( tentity entity, func<tentity, ilist<titem>> listgetter) tentity : class { using (var session = nhibernatehelper<t>.opensession()) { // here brand new - connected - instance of tentity tentity reattached = session.merge<tentity>(entity); nhibernate.nhibernateutil .initialize(listgetter.invoke(reattached)); return reattached; } } and calling side:
using (var session = nhibernatehelper<t>.opensession()) { ilist<foo> foos = session.createcriteria<foo>() ... .list<contact>(); } foo foo = foos.first(); // here merged instance initiated foo = foo.loadcollection(f => f.somecollection); note: depends why need that. merging need change reference (old detached object lost).
enough (or better) load collection ilist<titem> directly, via session, using parentid = current.id... idea...
Comments
Post a Comment