tags:

views:

105

answers:

1

I am writing a Catalyst web application that presents some data that does not change in between requests. I want to load this data when the application starts and stick them somewhere so that the relevant controllers can use them. Where should I load this data and where should I store it? At first I tried to load it in the main App.pm file and put them into the application config. This works, but it’s ugly, since the App.pm file gets littered by a lot of loading subs that don’t really belong together:

__PACKAGE__->config(this => load_this());
__PACKAGE__->config(that => load_that());
# et cetera

Then I figured I could load some of the data in the controller that uses them and store them in the controller’s config hash. This is not perfect either, because some of the data is needed in more than one controller.

I also thought I could localize the data to the controller instead of sharing them. This feels nice, because I would have all the code that works with the data more or less on one place. The problem is that the controller would also have to render parts of the resulting page for other controllers – for example if the title page controller wanted to display a list of recent news, it would have to ask the news controller to render the list. This does not seem to be the preferred way of doing things in Catalyst.

How would you solve this?

(The nature of the data: This is a website of a music festival. There is a line-up, a list of sponsors and other simple data pulled from YAML files, because database would be overkill and hard to edit.)

+3  A: 

Fundamentally, the data should belong to the Model, which is to say your YAML files should be represented by modules in your application's Model. That's one reason you separate the model from the controllers in the first place: All of your controllers can talk to the same models, without having to replicate their information within themselves.

There's no reason you can't cache the data persistently in these modules, using plain perl data structures or whatever caching technique you like.

Adam Bellaire
The files contain a list of items. Each item has a corresponding model class, but I was hesitant to create another class for the whole collection, since it would be simply a wrapper around a list. +1 anyway, since this is useful indeed.
zoul
@zoul: I think the problem you've encountered justifies the wrapper. If you're trying to persist the data across controllers in a way that makes sense, then that "view" of the data, so to speak, is important enough to be represented in the model.
Adam Bellaire