Must Everything Be Virtual With NHibernate? - That Extra Mile:



But, whenever you access any of the non-identifier members (that means properties and methods) of a proxy instance, NHibernate needs to make sure that the data of either the Customer or the Employee (depending on which one you're using) needs to be fetched from the database. So how does NHibernate do that? The proxies will override all of your properties and methods and when one of them is accessed, NHibernate will either fetch the data of the entity if it's not present yet and then proceed with the original implementation of the property or the method, or it will immediately call the original implementation if the data was already present.


Wish that post had some code to show what he was talking about (that is, code that shows what the NHibernate proxy looks like), not because it's difficult to understand, but it's even easier, I think, than reading about it.



I just wanted to add some methods, which seems like it shouldn't have any influence on what data's pulled at all, but, of course, you have to have pulled your data for the entity has any of its methods called.  I've got to assume the NHibernate proxy just adds a wrapper of "Has data been loaded? If not, lazy-load it before you really call this method" to each method.  What a boring bit of overhead, but as Brion says...



There are other ORM's that don't require you to make your members
virtual and they are still able to offer lazy loading features. But
those ORM's usually require you to either inherit from a specified base
class, or to implement one or more interfaces that the ORM will use.


The overhead's coming out somewhere if you don't want to aggressively load your data.

Labels: