10 March 2014
The Repository interface
Here is the definition of a repository for Eric Evans in his Domain Driven Design book:
A repository represents all objects of a certain type as a conceptual set (usually emulated). It acts like a collection, except with more elaborate querying capability. […] For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type.
While I love Doctrine, I really dislike
their repository interface
because it doesn’t look and act like a collection.
And that’s too bad because Doctrine even provides a very good abstraction for collections
through the doctrine/collection
project. It even supports filtering with criterias over collections and repositories.
I know that Doctrine is not targeted at Domain Driven Design only, but I think having a better repository interface would still benefit the project and the users.
Here is a basic repository interface I tend to use instead:
interface Repository
{
function add($entity);
function remove($entity);
function count();
/**
* @throws NotFoundException
* @return object
*/
function get($id);
/**
* @return array
*/
function find(Criteria $criteria);
function toArray();
}