Data Mapper
Escrito por Darjus Kazakevic · revisado el
Objects on one side, tables on the other, and one layer that translates.
A domain model is shaped by business rules. A database schema is shaped by storage: tables, foreign keys, indexes. A Data Mapper is a separate layer that translates between the two, so neither side knows about the other. The domain class has no save() method, no base class and no ORM annotations.
The alternative is Active Record, where the object is the row and saves itself. Active Record is less code and fine for simple CRUD. A Data Mapper costs more mapping code, but it lets a rich model and its tables change independently. Most repositories are built on one.
En esencia — Put all mapping between objects and tables in a separate layer, so the domain model and the schema can each change without the other.
Cuándo recurrir a él
Recurre a él cuando
- A rich domain model with real business rules
- The table shape and the object shape really differ
- A legacy schema you cannot change but must model cleanly
Reconsidéralo cuando
- Simple CRUD screens, where Active Record is less code
- Read-heavy screens better served by a direct query
- Small apps where tables and objects will always match
The moving parts
- The persistence-ignorant model
- Domain classes that know nothing about storage: no ORM imports, no annotations, no save(). You can create and test them with no database anywhere.
- The mapping
- The one place that knows which field goes in which column, how a value object is flattened into columns, and how an aggregate's child rows are loaded.
- Reconstitution vs. creation
- Loading an object is not the same as creating one. Creation checks invariants (rules that must always hold) and may raise events. Reconstitution rebuilds a state that was already valid, so it does neither.
Applying it well
- Map whole aggregates, not single tables
- Load and save the aggregate (a cluster of objects saved as one unit) with all its children. A mapper per table pushes assembly into the application layer, where nothing enforces the aggregate's rules.
- Design the model first, then map it
- Shape the classes from business rules, then map them to whatever schema you have. If a field exists only because the table has a column, the mapper has stopped doing its job.
- Keep the ORM inside the mapping layer
- A mature ORM such as Hibernate, Doctrine or TypeORM is a good data mapper, so use it. But keep its annotations, lazy-loading proxies and sessions out of the domain classes.
- Send reporting queries elsewhere
- The mapper loads and saves aggregates by id. Screens and reports should query a read model or a view directly. Bending the mapper to serve them adds flags and half-loaded objects.
Errores habituales
- ORM annotations on domain classes. The mapping has leaked into the model.
- One mapper per table, so the application layer assembles aggregates by hand.
- Lazy-loading proxies inside the domain that run queries nobody sees in the code.
- Domain events that fire on every load, because loading reuses the creation path.