.net - Howto ensure objects are lazy loaded with (Fluent) NHibernate? -
i have application using fluent nhibernate on server side configure database. fluent uses lazyloading default, explicitly disabled this gave me problems when sending objects client side. client can't load objects lazily doesn't have access database.
now try reenabling lazyloading parts of datamodel there parts want return toplevel objects client. however, don't seem lazyloaded. why?!
what did disable lazyloading adding not.lazyloading()
in mapping object, , on references in mapping. removing doesn't seem have effect. debugging see referenced objects, , them on client side. however, nhibernateutil.isinitialized(myobjectfromdb.somereference)
correctly says false @ same time. so; how ensure objects lazy-loaded; getting object missing references client? ideas might got wrong?
i have few classes (very simplified example..):
public class customer { public virtual int id { get; set; } public virtual string name { get; set; } public virtual ilist<order> orders { get; set; } } public class order { public virtual int id { get; set; } public virtual ilist<item> items { get; set; } } public class item { public virtual int id { get; set; } public virtual string name { get; set; } }
simple mappings - using default lazyloading:
public class customermapping : classmap<customer> { public customermapping() { id(c => c.id); map(c => c.name); hasmany(c => c.orders); } } public class ordermapping : classmap<order> { public ordermapping() { id(c => c.id); hasmany(c => c.items); } } public class itemmapping : classmap<item> { public itemmapping() { id(c => c.id); map(c => c.name); } }
and fetch straight forward session.load<customer>(id)
- returning result on rest service directly without accessing object such lazy references loaded. both right after load , on object returned server side references loaded. how can prevent this?
did more research , testing. lazy loading turns out simple expected. default behavior in mappings, not saying cause objects lazy loaded.
the question is: when loaded? - if lazy loaded loaded whenever needs them - within scope of session. so; inspecting object in debug-mode check if loaded in fact trigger loading of object - lazily. inspector show references loaded, , fine.
the problem in case mentioned "within session scope". have client , server side. server provides rest services, , client calls these using httpclient. on server data wrapped in xml, , returned. in case session still lives when return result - meaning wrapping of data load references lazily - when needed. references, , complete object returned client.
so; ensure lazy loading fluent nhibernate don't set explicitly, , aware if use objects references in way cause them loaded.
Comments
Post a Comment