Comparison
Identity mapvsUnit of work
Identity map
you loaded the same customer twice in one request and got the same object, not two equal ones.
A per-transaction map from database identity to loaded object, so a row read twice produces one instance. It removes a whole class of bug where two copies of the same record drift apart, and it makes reference equality meaningful within a unit of work. It also explains a common confusion: a second query for a row already loaded may return the in-memory version, complete with your uncommitted changes.
Full entry →Unit of work
you changed four objects and called save once, and it worked out the order the writes had to happen in.
An object that tracks everything changed during a business transaction and coordinates writing it out, including ordering and concurrency checks. It is what a session or context in an ORM is, and it is why calling save on one object can produce eight statements. Its two consistent surprises are the flush that happens earlier than you expected, and the fact that a long-lived unit of work is a long-lived pile of tracked state.
Full entry →