I am working on a J2EE project using struts and EJBs to implement an MVC structured application. The application has three principal entities, which to simplify this question, we'll describe as follows:
- A MusicLibrarys represent a particular user's collection of music, and contains...
- Albums, which in turn contain...
- Songs.
Each entity, of course, contains additional related data (i.e. the MusicLibrary is associated with a user, so it might have its name, Albums have titles, etc.).
Okay, so that's the model... Now...
I need to create a page which prints all of the data for a particular MusicLibrary, including all the contained Albums and in turn, the songs on each album. As I understand it, my controller (Action) needs to use the model to retrieve this information and place it in some scope my JSP has access to (likely the request).
In a model 1 application, I would simply send the information needed to identify a particular MusicLibrary and from there, the page would retrieve all the Albums from the database, and loop through that recordset, locating all of the songs for each Album. In my MVC pattern, however, the controller needs to retrieve all this information up front and pass it on to the view in a form that allows me to retain the "ownership" relationships of the entities.
I've considered a number of approaches, all of which have advantages and disadvantages. For example:
- Directly retrieve a JDBC RecordSet containing all of the information for each song (including MusicLibrary and Album information) and translate the RecordSet into a Collection of value objects. This is probably the simplest solution for the Model, but will mean a good bit more complexity in my JSP.
- Pass the MusicLibrary entity directly as a single value, and then pass a Collection of value objects, each of which contains a reference to an Album and a Collection of Songs. This is what I've been leaning toward, but I am concerned about how much load this will create as all these entities are created (after all, people tend to have very large music collections!)
- Follow the same approach as 2, but flatten the Collection so that the Albums and Songs are all contained within it and a new group on the page is indicated by finding an Album object rather than a Song. This seems to be a good compromise, but does not create very clear code, and is very sensitive to the ordering of elements within the Collection.
So, in general, how should complex data be passed in an MVC framework?
Any ideas? Suggestions? Ridicule?