views:

43

answers:

1

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?

+1  A: 

Did some more research and testing. Lazy Loading turns out to be as simple as expected. This is the default behavior in the mappings, so not saying anything about it will actually cause the objects to be Lazy Loaded.

The question is: When are they loaded? Well - if they are Lazy Loaded they will be loaded whenever anyone needs them - within the scope of the session. So; inspecting the object in Debug-mode to check if it is loaded will in fact trigger loading of the object - lazily. So the inspector will show the references as loaded, and everything will look fine.

The problem in my case was what I mentioned about "within the session scope". I have a client and server side. The server provides REST services, and the client calls these using an HttpClient. On the server the data will be wrapped in XML, and then returned. In my case the session still lives when I return the result - meaning that the wrapping of the data will load the references lazily - when needed. So I get all the references, and the complete object is returned to the client.

So; to ensure Lazy Loading with Fluent NHibernate just don't set anything explicitly, and then be aware if you actually use the objects references in any way as this will cause them to be loaded.

stiank81