Quote:
The code submitting the initial query and the other UI logic shouldn't know or care if lazy loading is occuring or not. Am I expecting too much here?
No, that sounds right. The catch is, in order for lazy loading to work, the entity class in question needs to be attached to a session. Suppose Parent is an entity class with a lazy collection called Children. Then this will not work (code written in browser, should be treated as pseudocode):
Code:
public class PersonForm : System.Windows.Forms.Form
{
private Person m_person;
public PersonForm( int id )
{
ISession session = sessionfactory.OpenSession();
m_person = session.Get( typeof( Person ), id );
session.Close();
}
private void PersonForm_Load( object sender, EventArgs e )
{
textPersonName.Text = m_person.Name;
}
private void Button1_Click( object sender, EventArgs e )
{
StringBuilder children = new StringBuilder();
children.Append( "This person's children are: \n" );
foreach( object o in m_parent.Children ) /// Produces lazy loading exception
{
children.Append( ((Child) o).Name + "\n" );
}
textPersonChildren.Text = children.ToString();
}
}
But this code will work (ditto):
Code:
public class PersonForm : System.Windows.Forms.Form
{
private Person m_person;
public PersonForm( int id )
{
ISession session = sessionfactory.OpenSession();
m_person = session.Get( typeof( Person ), id );
session.Close();
}
private void PersonForm_Load( object sender, EventArgs e )
{
textPersonName.Text = m_person.Name;
}
private void Button1_Click( object sender, EventArgs e )
{
ISession session = sessionfactory.OpenSession();
session.SaveOrUpdate( m_parent );
StringBuilder children = new StringBuilder();
children.Append( "This person's children are: \n" );
foreach( object o in m_parent.Children ) /// no exception!
{
children.Append( ((Child) o).Name + "\n" );
}
textPersonChildren.Text = children.ToString();
session.Close();
}
}
Again, I've not compiled or tested this code, but I think that's the way it works.