Data Mapper Pattern
Concepts
- Background
- What is an ORM?
- Data Mapper Pattern
- Adapter Pattern
- Active Record Pattern
- Identity Map
Data Mapper Pattern
The Data Mapper Pattern is an architectural pattern introduced by Martin Fowler in his book Patterns of Enterprise Application Architecture. A Data Mapper is a type of Data Access Layer that performs bi-directional transfer of data between objects in memory and persistent storage. With the Data Mapper Pattern, the in-memory objects have no idea there is a database, and the database schema is unaware of any objects that use it. This separation is the main purpose of a Data Mapper.
Tip
An individual Data Mapper moves data between objects in memory and a database while keeping the objects and the database ignorant of each other, and while keeping the data independent of the Data Mapper itself.
Caution
You've almost certainly heard the terms Model and Resource used in relation to data management. These are heavily overloaded terms and are avoided in this documentation to prevent confusion. JSData components have specific names that aim to unambiguously describe their function.
JSData implements the Data Mapper Pattern with a Mapper
component. Mapper
is a constructor function. Instantiating Mapper
gives you a new Mapper
instance capable of executing JSData's core ORM functionality. The user.json
example above describes several MySQL tables: users
, phone_numbers
, profiles
, and organizations
. To perform CRUD operations against these four tables one would need four Mapper
instances—one for each table.
Tip
Rather than manually instantiating and managing potentially dozens or hundreds of
Mapper
instances, JSData provides aContainer
component which will manageMapper
instances for you.
Updated almost 8 years ago