Hi all,
When you are developing an MVC based website that will, for instance, be able to display profile information about a user, how do you usually model data if it's coming from a relational database so that it would still be useful if the persistent storage mechanism changes (perhaps to some SOAP or XML based webservice)?
For example let's consider these pretty common type of DB tables:
- User:
- id
- username
- password
- email
- firstname
- lastname
- dob
- signupDate
- Image:
- id
- userId (FK User.id)
- location
- caption
- created
- updated
And let's consider these web pages:
/user/fireeyedboy/
- only displays info from DB table User
/user/fireeyedboy/images
- displays some basic info from DB table User (e.g.: username, age)
- and displays all child images from table Image
For the latter: do you create a model class User that is able to (lazy)load Images also (aggregate them in some way) that your view will use? Or do you have two distinct models that your view will use?
First option would be something like:
$user = UserManager->getUserById( 1 ); // returns User
$images = $user->getImages(); // returns ImageList
Second would be more distinct, something like:
$user = UserManager->getUserById( 1 ); // returns User
$images = ImageManager->getImagesByUserId( $user->getId() ); // returns ImageList
In my opinion the last option is probably the most sensible when using relational databases, but when the data is coming from another persistent source (maybe some XML source), that might not make too much sense anymore. In that case the User data might be provided hierarchical.
How do you usually deal with this? If it's not too much trouble, please be as explicit as you could if you will (by giving a brief but clear example), especially if you use ORM or other known techniques, as I may not be too formiliar with these yet. I usually learn best with some short but clear examples instead of trying to piece together the puzzle from theory. Thank you in advance.