As confirmed during the latest article about MVC, one of the greatests discussions around the subject is: Where’s the fat going?
The discussion goes between the two layers, Model and Controller, and has caused a lot of buzz since the MVC boom. So lets take a closer look…
Remembering the principles
As the pattern describes, the Model is responsible for the data that will be feeded to the user through the view layer. The Controller gets the data from the model and forward to the view in the desired manner.
NOTE: I will use PHP to illustrate the cases. The examples are very simple to be easy to comprehend so you can apply to your language of choice.
The Fat Model
The Fat Model is what the “Fat Model, Skinny Controller” principle (pointed greatly by Alvaro) defends. Following strictly this proposed vision of MVC, the Fat Model is which fits best since it keeps the data-relative code (business logic) at the data-relative layer (Model) where the Controller exchanges data between the View and the processed data. It keeps the Controller “Skinny” (uhh.. sexy!) and ready for summer!
So to get a very dummy example:
UserController (Skinny one)
public function viewProfile($user) { $user = User::findByUsername($user); // example ORM model // ... here goes some code $user->raiseProfileView(); $this->render('user/profile.tpl'); // example view rendering } // end :: function :: viewProfile
UserModel (who’s got the pizza?)
public function raiseProfileViews() { $this->views++; $this->save(); } // end :: function :: raiseProfileViews
At this point of view we can even purpose that non-database data may be manipulated by the Model layer if its a heavy load or business logic.
NOTE: The Fat Model principle aids to stay DRY.
The Fat Controller
It’s another very commom practice in the code wilderness where the controller handle the business logic and some heavy stuff leaving the models strictly to the DBAL/ORM paper. Even not following exactly the pattern’s principle it’s is another point of view since the Controller actually “controls” everything.
UserController (Chubby one)
private function raiseProfileView($user) { $user = User::findByUsername($user); // example ORM model $user->views++; $user->save(); } // end :: function :: raiseProfileView public function viewProfile($user) { $user = User::findByUsername($user); // example ORM model // ... here goes some code $this->raiseProfileView($user); // ... more code $this->render('user/profile.tpl'); // example view rendering } // end :: function :: viewProfile
…and this way we don’t bother the Model so she can keep watching to soup operas and painting her toes.
To be, or not to be strict?
Hail the evangelists! This is wood for fire absolutely. It comes from coder to coder, and that I leave that to you.
And what’s your style?
My style is fat model…
but I have trouble implement this in CodeIgniter especially when I need another model in a model (I hope you understand what I mean), CI can not load another model in a model (normally). So if you work a controller with 2 or more models you must balance them (fat controller and fat models).
I haven’t implement this concept, I still use fat model style to implement even the controller has more than 2 models and find my code…. very dirty, bad and hard to read…
so I suggest you using balanced one…
Chandra Kharisma
December 25, 2008 at 11:22 am
I agree with you Seidl, one should always balance their application, be it fat or skinny.
It should be whatever works the best for you, even if sometimes it doesn’t adhere to strict MVC principles.
You could sum this issue up with this acronym: KISS (Keep it Simple Stupid)
@Chandra: Code Igniter is great, however its a framework with a small footprint (as you already know). If you are into php then go the Cake or Zend route :=> even consider the Rails Way, you might just love the core concepts like “convention over configuration”, “Restful” and “Dry” practices.
Ahad Bokhari
December 27, 2008 at 8:09 am