Posted on 2/9/08 by Felix Geisendörfer
this is post #14 of my 30 day challenge.
In my previous post people were wondering why I had chosen a datasource for my implementation of the Akismet API. Some people felt Models, Components or even Behaviors could be more appropriate.
Indeed, about 2 years ago I did a previous Akismet implementation using a Model. But that always felt wrong. In CakePHP Models are meant to abstract a relational database table. To map its associations, to define its validation rules and provide easy CRUD functionality.
Unless the web service you're dealing with is a REST interface for CRUD operations, using a Model for its abstraction is way overkill.
So what about Behaviors? Behaviors are meant to abstract re-usable CRUD, validation and association tasks. A behavior might interact with a web service, but should not be the primary means of abstracting it.
Components are tempting. They indeed seem like a good choice for certain tasks. But their strength lays elsewhere. Components are good for abstracting functionality shared among multiple controllers. Often times they aren't even necessary because the logic one is trying to abstract has its place inside the model. Also:
Components are not globally accessible nor do they have a built-in mechanism for configuration like datasources do.
So I am fairly convinced that most web services are best abstracted using datasources. They make it easy to store the API key in your database.php file (which is not semantically perfect but lets be a little forgiving here). They can be accessed from a Component, Controller, Model, Behavior or any other part of your application and that makes a ton of sense. Because some services may allow you to upload data, resize pictures, transform text, detect spam, aggregate information, validate identities - all things that are best done in different places of your application.
There are some imperfections to the whole thing and some web servies might require the combination of a datasource (for the protocol) and a model (for the CRUD abstraction), but if you are in doubt - start with a datasource.
If you are interested in learning, check out some of the datasources we have published so far:
-- Felix Geisendörfer
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.