NHibernate Pitfalls: Lazy Scalar Properties Must Be Auto
Posted
by Ricardo Peres
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Ricardo Peres
Published on Fri, 06 Jul 2012 07:49:10 GMT
Indexed on
2012/07/06
9:16 UTC
Read the original article
Hit count: 313
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.
© ASP.net Weblogs or respective owner