Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the impact of lazy="false" on class element of NHibernate mapping?

I'm working with a legacy system that I'm experimenting with adding NHibernate to. I have class that I need to be mapped to a table, but it has lots of existing methods that are not virtual.

I discovered that I can get NHibernate to load the mapping successfully even with the non-virtual methods present if I set the "lazy" attribute on the class element of the mapping file to "false". I'm wondering what impact this will have on my use of NHibernate with this class.

I understand the meaning of non-lazy loading on collections that belong to an object, but I'm not clear on the meaning of what lazy or eager loading on a class would be. Does that mean all collections belonging to that object would be eager-loaded? Or does it mean that NHibernate no longer uses a dynamic proxy in place of the actual class? Something else?

Also, what's the best course of action here? Is setting that lazy=false value inadvisable? Should I create an interface that the class implements, and then map that to the table? Or should I just bite the bullet and mark all the existing methods on the class virtual?

Thanks in advance for any and all advice!

like image 389
Brian Sullivan Avatar asked Mar 03 '09 14:03

Brian Sullivan


1 Answers

I always specify lazy=false on the class level in NHIbernate, because I do not want that NHibernate forces me to declare that properties are to be virtual, if I do not want this in my class-model.

When you specify 'lazy' at the class mapping (default), NHibernate uses a 'Dynamic Proxy' class at runtime. This Dynamic Proxy is a class that inherits from your class. Then, as far as I understand, the instance of the class should be initialized lazily / on-demand. In some cases, this should be better for performance (at least, that's what's being told).

But, since I do not like that NHibernate tells me how my class should look like, I've always specified lazy=false for all my classes, and I haven't ran into trouble yet. :)

like image 74
Frederik Gheysels Avatar answered Nov 06 '22 01:11

Frederik Gheysels