Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

NHibernate Pitfalls: Lazy Properties in Non-Lazy Entities

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

If you want to have lazy properties on your entity, your entities must also be lazy. This applies to basic type or array type properties (typically String or Byte[] mapped to CLOB or BLOB columns) as well as entity type references (one to one, many to one). This is because NHibernate will only create proxies (in practice, subclasses created at runtime that override the lazy properties) for entities marked as lazy. These properties, of course, must be declared as virtual, and need to be public, protected or protected internal, private or internal wont work, and the classes cannot be sealed.

A different matter is lazy collections (one to many): for these, classes don’t need to be inherited from as NHibernate will change the collection properties for their own collection classes that already implements the lazy behavior. These collection properties must though be declared as interfaces (IEnumerable<T>, ICollection<T>, IList<T>, Iesi.Collections.Generic.ISet<T>), not actual types, so that they can be assigned to NHibernate classes, which also implement these interfaces.

Bookmark and Share

Comments

Ray Akkanson said:

Can IQueryable<> interface be assigned to NHibernate  classes?

Ray Akkanson

# May 29, 2011 12:02 PM

Ricardo Peres said:

@Ray:

Nopes. But there's a proposed patch: 216.121.112.228/.../NH-2319.

What exactly do you want?

# May 29, 2011 12:56 PM