I have an application using Fluent NHibernate on the server side to configure the database. Fluent uses Lazyloading as default, but I explicitly disabled this as this gave me problems when sending the objects to the client side. Obviously the client can't load the objects lazily as it doesn't have access to the database.
Now I try reenabling Lazyloading for parts of my datamodel as there are some parts where I only want to return toplevel objects to the client. However, they don't seem to be Lazyloaded. Why?!
What I did to disable LazyLoading was adding Not.LazyLoading()
in the mapping object, and on references in the mapping. Now removing this doesn't seem to have effect. Debugging I see all the referenced objects, and I also get them all on the client side. However, the NHibernateUtil.IsInitialized(myObjectFromDb.SomeReference)
correctly says false at the same time. So; how do I ensure that the objects are lazy-loaded; getting a object missing its references back to the client? Any ideas what I might got wrong?
I have a few classes (very simplified example..):
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Order> Orders { get; set; }
}
public class Order
{
public virtual int Id { get; set; }
public virtual IList<Item> Items { get; set; }
}
public class Item
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
Simple mappings - using default LazyLoading:
public class CustomerMapping : ClassMap<Customer>
{
public CustomerMapping()
{
Id(c => c.Id);
Map(c => c.Name);
HasMany(c => c.Orders);
}
}
public class OrderMapping : ClassMap<Order>
{
public OrderMapping()
{
Id(c => c.Id);
HasMany(c => c.Items);
}
}
public class ItemMapping : ClassMap<Item>
{
public ItemMapping()
{
Id(c => c.Id);
Map(c => c.Name);
}
}
And I fetch it straight forward with a Session.Load<Customer>(id)
- returning the result over my REST service directly without accessing the object such that the lazy references are loaded. Both right after the Load and on the object returned to the server side the references are loaded. How can I prevent this?