2

I'm trying to understand the DataMapper pattern by implementing it myself for one of my domain objects.

So far, I have:

class MyDomainObject
  <attributes>
  <business logic methods>

class MyDomainObjectMapper
  save
  update
  delete
  get

Now, I need get many MyDomainObjects. These could possibly be held cached in memory (saving many calls to the database). There could also be filter conditions.

Where does this functionality go, according to the pattern? I'm not sure if I should add methods to MyDomainObjectMapper or create a new class.

1 Answer 1

2

That's what an Identity Map is for.

Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.

For a full description see P of EAA page 195

http://martinfowler.com/eaaCatalog/idMapperSketch.gif

...An Identity Map keeps a record of all objects that have been read from the database in a single business transaction. Whenever you want an object, you check the Identity Map first to see if you already have it.

3
  • Interesting, I've never heard of that before. Can you explain more about which class is responsible for managing the map? (or maybe it's its own class?)
    – user39685
    Commented Apr 4, 2012 at 14:00
  • Look at martinfowler.com/eaaCatalog/identityMap.html Your mapper would use the identity map. Commented Apr 4, 2012 at 14:24
  • The Map is a class of its own. Commented Apr 4, 2012 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.