I was looking at ‘NHibernate 3 Beginner’s Guide’ book and found interesting tip:
In a real life inventory application, you will probably want to avoid
putting a Products collection on the Category entity, as it is
possible that a category can have hundreds, if not thousands, of
associated products. To load the whole, huge collection of products
for a given category would be unwise and would lead to an application
having unsatisfactory response times.
Tip was right after an example of one-to-many relationships building. The entities were Product and Category. The example is quite straightforward:
public class Category : Entity // Entity probably contains an `Id` property
{
private List<Products> products;
public String CategoryName { get; set; }
public String Description { get; set; }
public IEnumerable<Product> Products { get { return products; } }
}
public class Product : Entity
{
public Decimal UnitPrice { get; set; }
public String ProductName { get; set; }
public Category Category { get; set; }
}
So what is the real life example of one-to-many relationships?
Would it be enough for the example just to put Category inside a product entity as a String property?
The idea is that if you avoid the IList property all-together, you can still get to the products for a category e.g. like the following:
But you now have the possibility to do paging, filtering, getting the top product etc.:
which you do not have if it is a simple IList property. In general (in my experience), the less two-way ascociations you have, the better your querying granularity will be. Also, it reduces your complexity when it comes to saving.