Lazy loading does this automatically anyway, are you trying to avoid lazy loading? Why?
For instance:
IList<Customer> customers = _session.CreateCriteria<Customer>().List<Customer>();
Will provide you all customers. Orders will only be called from the database when you call the collection of orders for the Customer object, for example:
foreach(Customer customer in customers)
{
IList<Order> orders = customer.Orders;
}
, 10 , 11 (1, , 10 ). SELECT N + 1, , , "", . , , ?
: . , POCO -, POCO Session NHibernate, ( NULL). , "", - , NHibernate , :
public IList<CustomerView> GetAllCustomers()
{
return (from c in _session.Query<Customer>()
select new CustomerView()
{
Id = c.Id,
Name = c.Name
}).ToList();
}
public CustomerDetail GetCustomerFromId(int id)
{
return (from c in _session.Query<Customer>()
where c.Id == id
select new CustomerDetail()
{
Id = c.Id,
Name = c.Name
FullAddress = c.FormatFullAddress(),
Orders = c.Orders,
}).SingleOrDefault();
}
LINQ, NHibernate 3.0, NHibernate 3.0 Projections. , http://nhibernate.info/doc/nh/en/index.html#querycriteria-projection