advanced

Identity map

Keep one in-memory object per database row inside a request context to preserve identity and change tracking.

An identity map ensures one database row maps to one object instance within a request context. It supports change tracking and consistent object graphs in Data Mapper style ORMs.

					Request fork
  load Order#1 → instance A
  load Order#1 again → same instance A
				

On interviews: request scoping, entity identity, stale reads, serialization, and dangers of global entity managers.

Common pitfalls: sharing identity maps across requests leaking user state; detached entities behaving unlike plain DTOs.

The trade-off is object graph consistency versus scope discipline.

Checklist:

  • Scope identity map per request.
  • Know attached versus detached entities.
  • Avoid global mutable persistence state.
  • Clear context after request ends.