So I am new to nhiberate and have been reading the documentation about lazy loading trying to find a work around for lazy loading after a session is closed.
I found the
Lazy Loading topics
Which mentioned using session.lock and initialize
And so far as I have tested this seems to be working
I have a static convenience method that takes a persistant entity (IDataItem is my own protocol so don't worry about it) and an IList
Code:
public static IList ReattachLazyCollection(IDataItem anEntity,IList aCollection)
{
PersistentCollection tCollection =aCollection as PersistentCollection;
if (tCollection != null && !NHibernateUtil.IsInitialized(tCollection)) {
using (ISession tSession = Factory.OpenSession()) {
tSession.Lock(anEntity,LockMode.None);
NHibernateUtil.Initialize(tCollection);
}
}
return aCollection;
}
And it's called in my lazy IList getters such as
Code:
[NHMA.Bag(0,Lazy=true)]
[NHMA.Key(1,Column="PLAN_ROW_ID")]
[NHMA.OneToMany(2,ClassType=typeof(Participant))]
public IList Participants
{
get {
return Database.ReattachLazyCollection(this,_participants);
}
}
And since i'm using the field.camelcase-underscore property access, this doesn't seem to cause any trouble. So my question is for those who have an idea of what is going on underneath in Nhibernate, are there any downsides to doing this? Am I using ISession.Lock correctly?
Thanks, Jay