Reputation: 481
Let's say I have an object in PHP that is used in multiple places in one general representation. This object has several properties that could be either other objects or associative arrays, et cetera.
I am using the Data Mapper pattern to persist these objects, and while this is the first such complex object so far in this application, I'm wondering about the best way to integrate it with data mapper.
I'm wondering if it's a beneficial design decision to implement a public makeDBSafe() method to the object. For instance, the makeDBSafe() method serializes particular properties of the attribute which are too complex for normal SQL data types.
For instance:
$myObject = new MyObject($data);
// Application logic with $myObject
$myObject->makeDBSafe();
$MyObjectMapper->save($myObject);
The way I view it, if the database implementation has to change, then this doesn't affect how I choose to serialize the objects. If I want to change the actual serialized representation (ie JSON/XML whatever) then I would change it in only one place and per class. Also the Data Mapper would not know how the object is being serialized, it will only be provided with a "ready-to-save" object.
Upvotes: 0
Views: 68
Reputation: 546
If you read Martin Fowler's description of the Data Mapper pattern you would see that a domain object should know nothing about persistence layer. It is the responsibility of the Data Mapper to convert an in-memory object to persistence schema and back.
Upvotes: 1