Link round-up: 'Fat models, skinny controllers' design pattern for MVC web-development

When using a Model View Controller (MVC) web-development framework
such as CakePHP or Ruby on Rails, the natural tendency seems to be to
write most code (rules, calculations, conditions, etc) in controllers,
and to think of models as very convenient data access ("Yay, no more
writing SQL").

This creates 'fat controllers' and 'skinny models'. As the title of
this post hints at, there is a better way to do things: fat models and
skinny controllers. It is actually a simple change once you know about
it, and offers important benefits. We can also use software design
principles to justify why it is a better approach.

Here are some good links on the subject of fat models, skinny controllers:

http://www.mikebernat.com/blog/MVC_-_Fat_Models_and_Skinny_Controllers_

http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

http://gluei.com/blog/view/cakephp-best-practices-fat-models-and-skinny-controllers

http://www.littlehart.net/atthekeyboard/2007/04/27/fat-models-skinny-controllers/

http://www.mattwoodward.com/blog/index.cfm?event=showEntry&entryId=EBC6C06A-77A0-4186-BD321D6CB2509401

I recommend reading the comments on those sites too, as there are some
insightful comments.

So what are the good software design principles?

Logic should have only one home. When logic is placed
in controllers, it tends to get duplicated when the same logic is
needed elsewhere, leading to maintenance woes. Moving logic to a model
makes it easy to call from any controller (or another model).

Strong cohesion. It is very difficult to reuse a fat
controller as it is going to be doing a lot of different things
(meaning the controller has weak cohesion). A fat model offers many
different operations related to a specific data type, so it resembles
a library of related functions. This stronger cohesion increases the
opportunity to reuse a model without having to change it.

Another nice thing is how easily fat controllers can be refactored
into skinny controllers over time, moving code blocks to models. So
unlike a lot of design patterns, fat models and skinny controllers can
be introduced into established projects.

Filed under  //  CakePHP   Software design   Web dev  
Comments (0)
Posted