Reputation: 1200
I am starting an application built on zend framework; this application should be able to make use of multiple data sources other than a database; a webservice for example.
I have been reading on how to structure my model so as to allow for this scenario. I have come across various concepts that seem to provide a solution to this (DataMapper Pattern, Service Pattern, Adapter Layer, etc). However, I am still confused on how to put this all together into a reusable and scalable codebase.
I have worked with zend framework before and will normally work with a mysql table. If for example, I have a Users table...I simple have a Users class in my model that contains business logic of the the user domain and a User class extending Zend_Db_Table_Row_Abstract representing a row in the user table.
How best do I organize my model and code base such that i can still call $users->fetchAll() and get a collection of user objects regardless of what my datasource is?
Upvotes: 1
Views: 325
Reputation: 316939
It basically works the same as you did before, just that instead of a Zend_Db_Table_Gateway
you use a My_UserGateway_Whatever
, e.g. create an interface first:
interface UserGateway
{
/**
* @return array
* @throws UserGatewayException
*/
public function findAll();
}
We dont want Exceptions from the concrete Gateways to appear in the consuming code, so we add the UserGatewayException as a catch all:
class UserGatewayException extends RuntimeException
{}
Then add a class implementing that interface:
class My_UserGateway_Webservice implements UserGateway
{
public function findAll()
{
try {
// insert logic to fetch from the webservice
return $userData;
} catch (Exception $e) {
throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
}
}
// … more code
}
Likewise, if you want to use a Database source, you can write an adapter for the Zend_Db_* classes, e.g.
class My_UserGateway_Database implements UserGateway
{
private $tableDataGateway;
public function __construct(Zend_Db_Table_Abstract $tableDataGateway)
{
$this->tableDataGateway = $tableDataGateway;
}
public function findAll()
{
try {
return $this->tableDataGateway->select()->blah();
} catch (Exception $e) {
throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
}
}
// … more code
}
If you need another Data Provider, make sure they implement that interface, so you can rely on the findAll
method to be there. Make your consuming class depend on the interface, e.g.
class SomethingUsingUsers
{
private $userGateway;
public function __construct(UserGateway $userGateway)
{
$this->userGateway = $userGateway;
}
public function something()
{
try {
$users = $this->userGateway->findAll();
// do something with array of user data from gateway
} catch (UserGatewayException $e) {
// handle Exception
}
}
// … more code
}
Now, when you create SomethingUsingUsers
you can easily inject one or the other Gateway into the constructor and your code will work regardless of which Gateway you used:
$foo = SomethingUsingUsers(
new My_UserGateway_Database(
new Zend_Db_Table('User')
)
)
or, for the Webservice:
$foo = SomethingUsingUsers(
new My_UserGateway_Webservice(
// any additional dependencies
)
)
Upvotes: 4