Identity map pattern

From CSSEMediaWiki
Jump to: navigation, search

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

There are some obvious benefits from only loading each object once from the database - speed and concurrency.

How it works

A client will ask the finder class to retrieve a record from the database, the finder class will first check their own map, and if it doesn't exist, it will query the database for the record and store a copy locally. In a typical simple schema, there will be one map per database table. The design of the finder class needs to ensure that once all references to that object are complete, it is removed from the schema.

When to use it

Whenever you want to ensure that you don't have two in memory objects referring to the same (database) record. An identity map can help avoid update update conflicts within a single session.

Personal tools