Search code examples
model-view-controlleroopmodelimplementationconceptual

MVC, collection owner in the model or the controller ?


This questions is conceptual, around the MVC pattern in OOP. Here is the plot: We are in an rich client application which works with a REST webservice. We do a GET request to a ressource on the server, the server return a response formatted in XML. The response look like an RSS feed. The client application need to parse and display this XML into a readable mod.

I've made a model object, which basically reflect a server database table, I also have a parser and a view controller (and obviously a view).

Here is how it works today. The view controller tell the parser to start parsing, the parser (SAX type) read the XML, and instantiate a model object and load detail in it properties. Each time the parser is done with an item it notify the view controller troughs delegate method. It send the parsed item (the type is the model object) to the view controller. THe view controller add this item into a collection .

When the parser finished parsing the XML, it notify the view controller, then the view controller read each item if the collection and build the view.

Here are my questions.

  1. Is this a good implementation ?
  2. I think that the collection owner should be the model directly, so the view controller ask the model to start parsing, the parser notify the model instead of the view controller, and finally the model send back the collection to the view controller. Is this better ?
  3. Should I merge the model and the parser ?

Solution

  • Here are some points about your question:

    1. MVC comes in variations. There is no strict rule as to whether the Model or Controller should notify the view about the state change, or an event. It all depends on specifics of application.
    2. Communication between Parser and View, the way you described it first, is probably not the right way to do it. It is better that the view gets a populated collection, instead of communicating back and forth with the Parser to build it. This is not the job of the View. And also, to spread a single function (like building the collection) across multiple components is not a good practice.
    3. Whether to include the Parsing in the model, or have a separate component, a Parser? I suggest you think of the Model in isolation from other parts of the application (for example that the view needs to display it etc). In that isolation, see if parsing is something that naturally belongs to the Model. If so, then include it in the model. Else, make it a separate component. My guess is that it does not belong to the Model and should be a separate component.