I have a structure similar to this example.  On the store detail page
there is a list of the shelves in that store which display a Priced
>From value.  I can eagerly load the shelves since I know I will need
them, but to calculate the PricedFrom() it lazy loads the items on
each shelf which is obviously not ideal.  Most of what I have worked
with is pretty straight forward CRUD queries, but this could get ugly
and I would like to learn how to do something more advanced like this.

public class Store
{
    public Guid Id { get; set; }

    public IList<Shelf> Shelves { get; set; }
}

public class Shelf
{
    public Guid Id { get; set; }

    public Store Store { get; set; }

    public IList<Item> Items { get; set; }

    public decimal PricedFrom()
    {
        return Items.Min(i => i.Price);
    }
}

public class Item
{
    public Guid Id { get; set; }

    public Shelf { get; set; }

    public decimal Price { get; set; }
}

public class StoreRepository
{
    public Store GetByName(string Name)
    {
        var store= _session.CreateCriteria<Store>()
            .Add(Restrictions.Eq("Name", Name))
            .SetFetchMode("Shelves", FetchMode.Eager)
            .SetResultTransformer(new
NHibernate.Transform.DistinctRootEntityResultTransformer())
            .FutureValue<Store>();

        return store.Value;
    }
}

public StoreService
{
    public StoreDetailVM GetStoreDetails(string Name)
    {
        Store store;
        StoreDetailVM vm;
        using (var tx = _session.BeginTransaction())
        {
            store = _storeRepository.GetByName(Name);

            tx.Commit();
        }

        return Mapper.Map<Store, StoreDetailVM>(store);
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to