I often have the same trouble when I have to design my class for a web application. The requirements are : - maintainable (no copy-paste for instance) - layers fully separated (the business layer doesn't have to know which method of the data layer is used) - high performance : don't load useless data.
First I have a table with all my customers and their addresses : Code :
Customer
--Id
--Name
--Address
----City
----ZC
----Street
Now I want a table (in another page) with all my customers and the books that they bought, I have a few possibilities :
1/ I create a new class :
Code :
CustomerWithBooks
--Id
--Name
--Books[]
----ID
----name
PRO : I load only the useful data CONS : I build my class after my UI , and there is copy-paste.
2/ I add Books[] to the first class. PRO : Everything is in the same class, it's maintainable CONS : I load the address for nothing. If I don't load the address I can : lazy loading, but I really don't like it, or when I use my class I have to know which method of my DAL i called, and I don't like it.
3/ I use inheritance : Code :
ClientBase
--ID
--Name
ClientWithBooks : ClientBase
--Books[]
ClientWithAdress : ClientBase
--Address
PRO: really maintenable, and I don't load data for nothing CONS : What do I do if in one UI I want to show the Books AND the Address ?
4/ ?? I hope there is a perfect solution