NHibernate Pitfalls: Lazy Scalar Properties Must Be Auto

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

NHibernate supports lazy properties not just for associations (many to one, one to one, one to many, many to many) but also for scalar properties. This allows, for example, only loading a potentially large BLOB or CLOB from the database if and when it is necessary, that is, when the property is actually accessed. In order for this to work, other than having to be declared virtual, the property can’t have an explicitly declared backing field, it must be an auto property:

   1: public virtual String MyLongTextProperty
   2: {
   3:     get;
   4:     set;
   5: }
   6:  
   7: public virtual Byte [] MyLongPictureProperty
   8: {
   9:     get;
  10:     set;
  11: }

All lazy scalar properties are retrieved at the same time, when one of them is accessed.

                             

No Comments