I am writing a Data Mapper in PHP and am trying to work out how to implement an Identity Map when my domain objects follow Class Table Inheritance.
The problem, as I see it, is that an Identity Map cannot guarantee that data is manifested only once in the in-memory model because hierarchical objects are relying on hierarchical data.
For example, in my database I have a Parent table and a Child table. In my domain model I have a Parent class, and extending from that, a Child class. I can instantiate both a Parent object and a Child object, and record their identities in an Identity Map, no problems. If client code requests the same Parent or Child, I can return it from my cache, rather that the database, no problems.
But what happens if the Parent and the Child both relate to the same parent record? i.e. the same data in the Parent table? I now have that data represented twice in-memory, and if it’s modified in one (or both instances) I risk overwriting the changes.
It seems to me that somehow the Identity Map needs to follow a hierarchy similar to the Domain Objects and Mappers themselves, however it gets a bit complex at that point. Also, I have lots of Dependent Mappings to take into account too.
Any ideas/advice much appreciated!
I defined an interface, iQueueable, and only objects which implement this interface may be queued to have their changes written to the database.
Child objects have the full complement of object properties, so they implement iQueueable. Parent objects can not be queued.
This still allows parent and child object to fall out of synchronisation, but now there is only one source of database changes.