Comparison
Active recordvsData mapper
Active record
the object has a save method on it, so the thing that models an order also knows the table it lives in.
An object that wraps a row and carries both the domain logic and its own persistence — `user.save()`. It is fast to write and reads well when the object model and the table are close to a one-to-one match, which is most CRUD applications. The bill arrives when they diverge: the domain object cannot be tested without a database, cannot be modelled independently of the schema, and quietly acquires a second responsibility it can never give back.
Full entry →Data mapper
the domain object has no idea a database exists, and a separate class knows which column each field came from.
A layer that moves data between objects and the database while keeping them independent of each other and of itself. It is what makes a domain model testable without a database and free to differ from the schema — a class per table is not required, and neither is a table per class. The price is a mapping layer to maintain and a much larger gap between what you wrote and what the database sees.
Full entry →