tags:

views:

74

answers:

2

so i want to follow the mvc conventions but haven't really got the hang of it.

i've got problem understanding the explicit difference between a model and a library class and how they relate to each other.

eg. i want to create classes to add/edit contacts and also adding them to a group.

i thought it should be divided into 2 classes: Contact and Group.

The first class would create a contact. The other one will add the contact to a group. I thought this dividing would be perfect cause their logic is isolated from each others. Contact doesn't care if Group exists or not. Group doesn't care about how a Contact looks like.

So Contact will have these methods:

$Contact->add($name, $email, $address, $phone) // create an entry in database
$Contact->delete($id) // delete the entry in database
$Contact->edit($id, $name, $email, $address, $phone) // edit the entry in database

And Group:

$Group->createGroup($name) // create a group in database
$Group->delete($id) // delete a group in database
$Group->addContact($groupId, $contactId) // add a contact to a group in database

So these classes apparently work with the database. Does this mean that these are models? Or are they library classes that eg. should be put in SYSTEM/LIBRARIES in CodeIgniter. If it's the latter one, how do a Model in this case looks like using the classes?

And how would a controller look like in this scenario?

Would be great if someone could give me the big picture! Thanks!

+3  A: 

The MVC pattern does not have the concept of a Library. What you are refering to is a general usage pattern of MVC frameworks. That is having a model folder, which contains application specific classes often derived from generic, reusable and application independent library classes.

Gordon
yes maybe i was not clear about that point. i know mvc has nothing to do with library classes, so i wanted to know if those 2 classes were MODELS or LIBRARY classes that i should use in the models. cause i know that those 2 could be used in other applications, which make them more like independent library classes? so i should create one model class where i use these 2 library classes?
never_had_a_name
@ajsie Since the mentioned classes do things that make no sense outside this specific application, they are models in my book. You have written them for this very application. They are not abstractions, but concrete classes for a concrete usecase.
Gordon
@gordon, read my updated first-comment. hmm i thought they could be used for other applications to create an address book?
never_had_a_name
@ajsie yes, you could probably reuse them, but then they would be library classes in an addressbook framework. you have to understand the key difference. The model contains everything that makes up the heart of your application. It's all the business logic, service layers, database adapters and so on specific to your app. The library, so to say, is the model of the framework, used to built your app. Does that make sense?
Gordon
yes it does, just have difficulty to know the exact difference. could you say a rule of thumb to make it clear where the line goes between mvc classes and library classes is if you have to modify the code to fit your application, then it sure is a application-specific class, with other words, a mvc class. cause library classes dont have to be changed i guess? and as you said, i could reuse the address book as a component in other applications, and the database structure comes along, so then it should be called a component with mvc (and those 2 are models). im correct?
never_had_a_name
@ajsie yes, i think you could say it like that.
Gordon
+2  A: 

Generally Models are like objects, they interact with the database, have variables and functions, and are reusable.

Libraries are normally sets of helper functions to process data / perform actions.

Josh K
but libraries tend to be classes too right?
never_had_a_name