Over a million developers have joined DZone.

MVC - Fat Models and Skinny Controllers

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

I've been doing a lot of work with CakePHP lately and it has been amazingly satisfying developing in a framework that encourages great coding practices. More on CakePHP, specifically, later. Right now, I'd like to present some things I've learned about the architecture that CakePHP is built on. I'm talking about the Model-View-Controller (MVC) architecture of course.

I'm hoping most of you reading know already know a thing or two about MVC but if you don't, here is a quick overview from wikipedia.

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.


The view is pretty well-defined and self-explanatory, however the precise boundary between the controller and model is a grey area. It's very common to see simple models with a couple of basic query functions and a very large controller, much of which is spent rearranging, formatting, or massaging the data returned by the queries. Pádraic Brady in this article suggests, and I agree, that Models are the most mis-understood and underused component of the three. I have found that fat models stick to the values and mindset that the MVC initially laid out.

Models themselves are often misunderstood to be a simple database abstraction layer or active record component. Mistakes like these will usually lead to very ugly-looking, sloppy, code in the controller. The controller's purpose in the big picture is to be the middle-man between the view and the model. Passing request data from the view to the model, and returning a response. This should NOT include things like processing data from the model data before it gets sent to the view.

Each method in the controller is normally considered to be a single action in your application. This often  encourages developers to put all their code into that action, ignoring usual coding, design, and OOP,  standards. Instead, try placing those chunks of code into logical methods in your model. Doing this, in the long-run, makes much more logical sense because you can call that same method from any related models.

Here is a very straightforward image (Notice the size of the layers)

Cake MVC

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Mike Bernat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}