CodeIgniter Forums
Advantages of Models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Advantages of Models (/showthread.php?tid=14166)

Pages: 1 2 3


Advantages of Models - El Forum - 12-20-2008

[eluser]Andy Chapman[/eluser]
Hi all, I'm relatively new to proper MVC programming. I understand what each component is, and the benefits of using controllers and views. What I'm not sure about is the benefit of using models.

What is the advantage of creating a model that is separate from the controller? I'm guessing it comes down to code reuse, so for common database interactions you can write once and use the same model in different controllers.

There is probably also a benefit in keeping the logic of the controller very readable by removing database queries etc.

Any thoughts?


Advantages of Models - El Forum - 12-21-2008

[eluser]Artemis Mendrinos[/eluser]
What I have always in mind, is that models are the abstract world of my web application. I mean, you describe there all your application models (things that store something and do something with a specific way). This is an existance of something that has the power to acheive things (black box) at any time from your web application.

Now, through a controller (wich is a strict execution that starts up from an http request and ends up with an http response) you are taking all the behaviour/knowledge/power of your models and you are just describing what to do with them in order to serve JUST the specific request.

So try to keep in mind:

Controllers are here in order to serve a specific request as fast and simple as it can be.
Models are functionalities that assist controllers to do their work fast.
Models are actualy the abstract power of your application.
Personally I put controllers in the middle of Presentation and Logic Tier:
http://en.wikipedia.org/wiki/File:Overview_of_a_three-tier_application.png


Advantages of Models - El Forum - 12-21-2008

[eluser]Andy Chapman[/eluser]
Thanks Artemis, you explained that well, very helpful.


Advantages of Models - El Forum - 12-21-2008

[eluser]jalalski[/eluser]
My view of MVC is basically the same as Artemis, but to put it in different words:

The Model handles the data. Creating, modifying, saving etc of the data structures of the application.
The View handles displaying that data to the user and getting input through forms.
The Controller is the business logic of the application and as such is the heart of the application.

My 0.02c


Advantages of Models - El Forum - 12-21-2008

[eluser]sl3dg3hamm3r[/eluser]
[quote author="jalalski" date="1229880638"]The Controller is the business logic of the application and as such is the heart of the application.[/quote]

Some say that controllers should be as slim as possible, which would result in putting heavy business-logic into libraries and/or helpers.
But as for me, I only create such when I see some reusability...


Advantages of Models - El Forum - 12-21-2008

[eluser]Colin Williams[/eluser]
Quote:I’m guessing it comes down to code reuse
Quote:probably also a benefit in keeping the logic of the controller very readable

Answered your own question Smile


Advantages of Models - El Forum - 12-21-2008

[eluser]Andy Chapman[/eluser]
Quote:Answered your own question

:-) Still, some good insights by others above too which is helpful.


Advantages of Models - El Forum - 12-21-2008

[eluser]Colin Williams[/eluser]
Certainly. I wouldn't have been so short had that not been the case. I wouldn't, however, put any onus on keeping controller methods slim; sometimes there is a lot to do (think of a shopping cart checkout). Only abstract when it's smart. Don't abstract yourself into a corner.


Advantages of Models - El Forum - 12-21-2008

[eluser]Andy Chapman[/eluser]
Quote:Only abstract when it’s smart. Don’t abstract yourself into a corner.

Thanks Colin, I hear that. I'll be using controllers to handle all logic, as after all that is the whole point. Unless I can see a compelling reason, I wont be dividing my code up any more than the standard MVC pattern demands.


Advantages of Models - El Forum - 12-21-2008

[eluser]jalalski[/eluser]
If the controller is appearing a little bloated, it's a sign that it's time look at moving some stuff to a library, plugin or helper. Depends on what can be encapsulated easily (or not). A good example is the Form Validation, stuff that, as application logic, belongs in the Controller, but is much cleaner to have as a Library.