Skip to main content
2 of 2
added 496 characters in body
Ewan
  • 79.9k
  • 5
  • 83
  • 179

Usually with a Customer object I would fully populate it. Although you want the possibility of multiple addresses etc you normally have only one or two and you tend to be dealing with a single customer at a time.

However, if you start having many sub objects or are dealing with many customers at a time (say for a report) I would remove all the sub objects (I usually use poco to mean only simple objects) and expose the relationships via the repository.

ie

repo.GetAddressesForCustomer(customerId)

class Address 
{
    string CustomerId {get;set;}
    ...
}

This allows you to, limit your returned data to only that which is needed and also to optimise your queries for the specific dataset you want to retrieve.

Say for example we want to show a list of customers with active orders, showing id, name and postcode of primary address.

Loading the full object one by one would entail n+1 db queries, even if we lazy load via EF or some other clever ORM we could end up with an inefficient query with wierd joins. But if we retain control at the repository layer we can populate our view model with

repo.GetCustomersWithActiveOrders();
repo.GetPrimaryAddressesOfCustomersWithActiveOrders();
Ewan
  • 79.9k
  • 5
  • 83
  • 179