This is a classic problem that I could never seem to come up with a solution for that I was happy with. What would be considered an OO-elegant and DB-scalable approach to this problem?
Employee
- Name, Phone, Meta, etc
Company
- Meta, etc
- Employee[]
CompanyRepository (RDMBS)
- Company GetById(int)
- Company[] GetAll()
Approach #1: 'GetById' Selects all from 'tCompany' and left joins 'tEmployee'. Sql Select yields 12 rows. Returns a single company populated with 12 employees.
'GetAll' Same Select as above but returns 12,000,000 rows. Through creative loops and logic returns 1,000,000 companies each with 12 employees.
Approach #2: 'GetById' ... same as above
'GetAll'. Selects all from 'tCompany' but nothing from 'tEmployee'. Sql select yields 1,000,000 rows. Returns 1,000,000 companies but each with a null 'Employees' property.
Approach #3 ... split the domain into 'SimpleCompany' containing only meta and 'ComplexCompany' that inherits from 'SimpleCompany' but has a 'Employees' property. GetById returns 'ComplexCompany' and GetAll returns 'SimpleCompany' array.
... each smells bad for different reasons.