views:

595

answers:

2

I've tried to lazy-load a property in my domain model, but lazy loading doesn't work. (It is always loaded).

[Property(0, Column = "picture", Lazy=true)]
public virtual System.Byte[] Picture
{
       get { return picture; }
       set { picture = value; }
}

When reading the documentation here it says that it requires build-time bytecode instrumentation. What does this mean - and how can I get it ?

A: 

For lazy loading to work NHibernate makes use of interception (via dynamic objects). That means it wraps your call to Picture and when you first call Picture it it will load the property from the database.

For this to work it can use one of three types of Dynamic object frameworks:

  • Castle DynamicProxy
  • Linfu
  • Spring

When you download NHibernate there is another folder with three types of these dynamic object plugins and you need to copy three dlls to the nhibernate folder (where nhibernate.dll is) and set a property in your nhibernate configuration file.

<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

Ref: http://nhforge.org/blogs/nhibernate/archive/2008/11/09/nh2-1-0-bytecode-providers.aspx

HTH Alex

AlexDuggleby
I've tried it with Linfu and Spring - but it didn't lazy load the object with either.the picture object was always initialized (even if it wasn't accessed until then)
bernhardrusch
+1  A: 

I have you tried a collection rather then an array?

[Property(0, Column = "picture", Lazy=true)]
public virtual IList<System.Byte> Picture
{
       get { return picture; }
       set { picture = value; }
}
Aaron Fischer