1
votes

I have a standard Category domain entity which has corresponding children. i.e category and its corresponding sub categories. This is shown below. Pretty standard stuff really.

public class Category : Entity
{
    private IesiCollections.ISet<Category> _children;

    public Category()
    {
        _children = new HashedSet<Category>();
    }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Category Parent { get; private set; }
    public virtual ICollection<Category> Children { get { return _children; } }

    public virtual bool AddCategory(Category category)
    {
        if (category != null && _children.Add(category))
        {
            category.SetParent(this);
            return true;
        }

        return false;
    }

    public virtual bool RemoveCategory(Category category)
    {
        if (category != null && _children.Remove(category))
        {
            category.SetParent(null);
            return true;
        }

        return false;
    }
}

I am using NHibernate profiler to make sure everything is running optimally. However, when I come to removing a sub category by calling the "RemoveCategory" method and passing in a category it has to access the underlying collection named "_children" which initiates a lazy load of the "_children" collection.

This causes a "Unbounded result set" alert in NHProf, which makes sense because we have not specified a limit. It will load the entire collection. I would like to specify a limit as this collection could potentially get quite large.

I have looked at the example (shown below) on nhprof website regarding unbounded results but I can't see how to use this.

var order = session.Get(orderId);
var orderLines = session.CreateFilter(order.OrderLines, "")
    .SetFirstResult(0)
    .SetMaxResults(25)
    .List();

DoSomethingWithOrderLines(orderLines);

This seems useful when we are displaying data but I want to update my entity and ultimately persist it. Any ideas on how I can specify a limit on the lazy load of the collection?

Kind regards

Mohammad

1

1 Answers

0
votes

you can just avoid accessing the Children property completely, by doing something like this:

category.Parent = null;

I realize this is only a workaround, but if that's all you need, it's the simplest solution.