I've always worked with various ORM's in the past and placed all my logic inside my models regardless of it's nature - SQL, MongoDB queries & even fetching of remote JSON objects. But when it's necessary to ensure loose couplings to allow a high level of testability, the issues of this methodology quickly appears.
Today I've read about separating models into two parts, Domain objects
& Data mappers
.
If I understood it completely, Domain objects
are completely unaware of the storage used, and instead exists to handle business logic. Data mappers
on the other hand takes care of storing the data set in the Domain objects
to a set data storage.
I do however find it a bit hard to find a good, easy-to-understand example online on how to work with the DomainObjects & DataMappers in a real world example.
Would this (below shown code) be the appropriate way to work with DomainObjects & DataMappers in my code to store Users or have I gotten it all wrong in my head?
$user = new User_DO;
$userSave = new User_DM;
$userSave->store( $user->add(array('name' => 'John Doe')) );
class User_DO {
function add($array) {
if(!isset($array['name'])) {
throw new Exception("Name must be set");
}
return $array;
}
}
class User_DM {
function store($array) {
MyDatabase::execute("INSERT INTO...");
}
}