I have a domain object used in an identity map (the keys are the object's Id
property).
Pseudocode:
map = new Mapping();
map[domainObj.Id] = 'foo';
I observe the object to tell me when it has been saved to the database:
domainObj.bind('saved', function() {
map[domainObj.Id] = 'new foo!'
})
For new objects the Id
field is empty and not populated until it is saved to the database.
And therein lies my problem. In the case of new objects, the lookup "map[domainObj.Id]
" fails because the object's identity has changed after being saved!
What is the best way to use an object in an identity map like this?
Constraints:
- I am using a language that does not allow objects as keys in a mapping [JavaScript]
- The
Id
field must be generated on the server when saving a new object
Update
Thanks for the feedback. I really would like to have value objects mapped the same way as entities: just because the value object has not been saved does not mean it does not have a value to map to for my application. I am in fact mis-understanding what an identity map is (which, after re-reading the pattern definition, it turns out I am not truly using).
The solution I came up with is pretty much straight from Igor's answer: each object gets a random id assigned to it upon instantiation. It is unique, immutable and unchanging during the lifetime of the context in which the object lives. It is there for both entities from the db and new instantiations. The tweak for entities is that it is set to match the real identity of the object. This makes debugging a bit easier. This is the value I use as the key in the mapping.