views:

35

answers:

1

I know that I am for responsible for this in the code, but in a object-oriented and MVC sense, who should really load the model. I have a model for representing RSS feeds that is being loaded from a url. The model is really thin and the controller manages downloading and parsing of the XML and then passes it on to the model. Is it better to do it that way or let the model be responsible for initializing itself?

The code in the controller that manages retrieving feeds looks something like this:

// EntriesController.m
- (void)getFeedsFromWeb {
    Parser *parser = [[Parser alloc] init];
    [parser addListener:self];
    Downloader *downloader = [[Downloader alloc] initWithParser:parser];
    [downloader getFeed:@"http://www.example.com/rss"];
    [parser release];
}

- (void)notify:(Feed *)aFeed {
    self.feed = aFeed;
    [self updateView];
}

The Downloader begins downloading and accumulating the response data, and passes it on to the Parser. When the Parser is done, it calls a callback method notify:(Feed *)feed in the EntriesController class, which updates it's model.

I don't think I am really decoupling the responsibilities in the Controller here, so I'm thinking of putting all this responsibility in the model itself. Add methods like

// Feed.h
- (void)initWithContentsOfURL:(NSURL *)feedURL
- (void)initWithContentsOfDatabase:(sqlite *)database

which does all the work in the model class itself. Is this a good approach and should the model be responsible for fetching data from various data sources?

A: 

Is "EntriesController" here a view controller or really a model controller? If it's a view controller, I would recommend pushing the logic into the Model class. The view controller's real job is to manage the view and coordinate with the Model; not to really control the model.

If it's really a model controller (i.e., it has no UI elements), then it's a little unusual in my experience to have both a controller and a model class that you've written. Generally all the controller logic could be put into the model, and the controller is just an unneeded layer.

My experience, generally speaking, is that in the MVC world, you tend to write two of them, and Apple (or a XIB) provides the other. If you're writing all three yourself, you may have too many layers involved.

Rob Napier
EntriesController is indeed a view controller, a table view controller. If there were multiple views for this model, the model initialization code would be replicated everywhere. And moreover, the model is unusable without the view controller. Thanks Rob, I see the massive flaw in my design now.
Anurag