views:

274

answers:

5

I am new to MVC but I can already see its benefits and advantages. However, I have a (probably easy to answer) design question:

I have been thinking about models and debating the proper way to structure them. The way I see it there are a few options:

1) Models and table structure have a 1 to 1 relationship...meaning that pretty much for every table there is a corresponding model. The model class has attributes corresponding to the table columns and has whatever methods that are needed (like getters and setters) to manipulate data in the table in whatever way is necessary. This seems like the generic option and I guess I would then have the controller call the models as necessary to perform whatever business function is necessary.

2) Models are tied more closely to the operation of the business logic rather than the data: so for example if on the front end a deletion of a certain object affects multiple tables, the model then 'models' this behavior and interacts with several tables and performs the necessary function. The controller then simply needs to call a single model for whatever business behavior is desired. This is less generic since the models are much more tightly coupled..but seems quicker to implement.

3) Something in between the first 2 options. Or maybe I am completely missing the point.

Hopefully this makes sense! If I am not totally missing the point, I am inclined to think that option (1) is better. Any idea?

Edit: Not that it should matter, but I plan on using Codeigniter PHP MVC framework.

A: 

Number 1 is the way to go. Option 2 is really the controller's job. For example, the controller then takes the models and performs actions on them, and passes the results to the view.

Think of it this way:

Model = your data

Controller = business logic

View = display of data and actions

This is highly simplistic, but it's how I picture it in my mind when I go to design a system.

AlbertoPL
Wow, beat me to the punch by a few seconds! :-)
McWafflestix
and following that, repositories==data access layer
David Archer
Disagree. If your model has an Employee object that contains a reference to a Boss object, why wouldn't your model fetch both when the Employee is populated, rather than making the controller populate the Employee manually?
womp
I mean I disagree that #1 is always the way to go ;)
womp
I agree with you womp, sometimes you do want your model handling minor business logic that makes sense. I'd have to say in most applications it's very hard to strictly stick to any of the paradigms.
AlbertoPL
A: 

Think of the database as the Model, the business logic as the Controller, and the UI as the View. That may help. It's an overly simplified approach to things, but it gets the data / behavior separation roughly correct.

McWafflestix
+1  A: 

All of the above.

The approach you use depends on your design philosophy. If you prefer to design your application using business domains and drive that into the database design, then you favor the second approach. If you prefer to build your database first, and then create model classes from the database schema, then you favor the first approach. Both methods are valid ways to build software.

Robert Harvey
+5  A: 

Both are valid implementations, and, depending on your needs, can work well.

Your #1 is essentially describing the Active Record pattern, which is used by SubSonic, Castle, and lots of other ORM implementations.

Your #2 is essentially describing the Entity Framework/Hibernate/LightSpeed approach, where you are dealing with objects that are more conceptually related to your domain rather than to tables. Instead of your objects containing foreign key ID properties, they actually contain the other domain object references, which are then instantiated in an on-access basis.

Both ways are great. The Active Record approach is usually more intuitive for beginners and has potentially less pitfalls. EF-style can save a lot of base-level coding and dealing with FK's directly in code.

Edit: To be clear, what you describe in both situations is data access layer related, rather then strictly model related. However in reality you're pretty close, as most models tend to simply represent one or more of these types of objects.

womp
A: 

I don't think it has to be an either/or situation. Your first point is what would be called a Model, but your 2nd point sounds like a View Model, which is most often a composition of various Models and parts of Models that will be sent to the view. The controller is responsible for doing that composition and potentially decomposition when information is sent back from the View.

Dennis Palmer