问题
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 ?
回答1:
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; }
}
回答2:
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
来源:https://stackoverflow.com/questions/1497179/nhibernate-lazy-loading-property-what-does-build-time-bytecode-instrumentation