nathanb
nathanb

Reputation: 67

MVC Pattern for web application - How to handle retrieving and manipulating lists of models?

I am developing a web application and I am implementing my own MVC architecture. I have the base Model, View, and Controller classes set up and working. These work great for displaying or manipulating single objects.

However, I have run into the problem of how to display (and manipulate) lists of these objects.

For simplicity's sake, let's say I have a Model_Event model which contains attributes about the event like id, name and date. I want to list all events in the database.

Should I have a controller separate from Controller_Event called Controller_Event_List, and should I have a separate model Model_Event_List that populates and contains an array of Model_Event objects?

Or should I use the same controller I use for single event objects and just have a method in Model_Event that returns an array of event data to display?

Upvotes: 0

Views: 115

Answers (1)

skuro
skuro

Reputation: 13514

Deciding whether to use one or multiple controllers is something application specific, you should ask yourself which is better suiting your application needs in terms of interface convenience for clients/users and server side code reuse/DRY.

Since you can always consider single results as a special case for lists you are always able to leverage lists as the one and only abstraction to use throughout the code. Moreover, you should already have a service layer underneath your controllers that does the actual fetching of objects from the DB, which is also subject to the same design choices.

My taste would probably be for single-service, multiple-controllers solution, but it really depends to application specific details (navigation rules, use cases, ...).

Upvotes: 1

Related Questions