Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How coarse grained should the model be in an MVC framework?

I have been reading a few questions previously asked and I haven't come across one that answers my question in "black and white" for me! So, apologies if this is repetitive. The question is probably similar to asking, "how long is a piece of string" but bear with me!

For a registernation system, I have a user model with functions such as:

  • add_user
  • delete_user
  • activate_user

The above user model deals with one table. The users table in the MySQL database.

You can guess what each function does but is this coarse enough? I mean should my model contain methods that are much broader such as:

  • add_record
  • delete_record
  • update_record

Where I pass in the table and a unique identifier of the record to delete, add or update?

I am using codeigniter, but I am interested in how things should be done in a pure MVC framework.

I apologise if this question is too picky.

Thanks all

like image 396
Abs Avatar asked Feb 13 '10 22:02

Abs


1 Answers

I'm not sure what you mean by "coarse".

"should my model contain methods that are much broader such as: add_record, delete_record, update_record"

Absolutely not. Never. That defeats the purpose of having a model.

That kind of "general-purpose" stuff is what a database is for. The point of a model is to adapt the general database to your specific problem.

Your model should be specific to your problem.

"user model with functions such as: add_user, delete_user, activate_user" That's the point. Your model reflects your application, your problem domain, your solution.

Your model should be able to -- in effect -- stand alone. You should be able to wrap your model in a command-line app or a GUI app or a web page.

like image 97
S.Lott Avatar answered Oct 10 '22 22:10

S.Lott