MVC: passing complex model data


General J2EE: MVC: passing complex model data

  1. MVC: passing complex model data (4 messages)

    I posted this on an earlier thread, but I wanted to rewrite it to give a better idea of what I want to accomplish.

    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:
    1. 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.
    3. 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!)
    5. 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?

    Threaded Messages (4)

  2. MVC: passing complex model data[ Go to top ]

    Hi Aaron-

    I'm working on an application with many of the same issues. I think that you should consider creating transfer objects that provide a coarse-grained view of the data. For instance, when you probably don't need to pass all of the fields for each song to the view. You can create a bean that will have a much smaller footprint than passing a full-fledged Song object. In turn, you can do the same thing for albums.

    For example:

    You can create the following classes:

    class SongTransfer {
      String title;
      // some other fields

      // standard bean set/get methods

    class AlbumTransfer {
      SongTransfer[] songs;
      String albumTitle;
      String artistName;

      // standard getters/setters

    class LibraryTransfer {
      AlbumTransfer[] albums;
      // some meta data fields for the library

      // standard stuff

    You can then extend the model with methods for grabbing only the information needed to populate the transfer objects. This way, you also enforce a stricter seperation between the model and the view. Some people might call this overkill, but for my personal situation it makes sense, given the complexity of the linkages between different entities in our model.

    Hope that helps,

  3. MVC: passing complex model data[ Go to top ]

    Thanks for your reply.
    You can then extend the model with methods for grabbing only the information needed to populate the transfer objects. This way, you also enforce a stricter seperation between the model and the view.

    You lost me on this part. Does this mean I should create model components (State Session Beans, perhaps) that will build and populate the transfer objects?

    Also, where does that place the transfer objects in the MVC framework?
  4. MVC: passing complex model data[ Go to top ]

    Sorry, I should have been more specific. Yes, you create model components to build and populate the transfer objects. The model for the application I'm currently building is basically a delegate that passes requests for data to various DAO (data access objects). The DAOs create and populate the transfer objects, which are returned to the view inside the HTTPRequest object. So, you can think of transfer objects as a 'pipe' between the model and the view. If you haven't already, you'll want to take a look at the core j2ee patterns. Take it with a grain of salt: some of the patterns can be implemented together in the same object, and the explanations are frequently EJB-centric, but these are important ideas in building an application using MVC.
  5. MVC: passing complex model data[ Go to top ]

    Thanks for all your help. This has given me a lot to chew on.