Skip to main content

Data Mapper

BehavioralData accessDecouplingObject mappingPersistenceAbout 3 min

Also known as

  • Object-Relational Mapping (ORM)

Intent

The Data Mapper pattern aims to create an abstraction layer between the database and the business logic, allowing them to evolve independently. It maps data from the database objects to in-memory data structures and vice versa, minimizing direct dependencies between the application's core logic and the underlying database structure.

Explanation

Real world example

When a user accesses a specific web page through a browser, he only needs to do several operations to the browser. The browser and the server will take the responsibility of saving data separately. You don't need to know the existence of the server or how to operate the server.

In plain words

A layer of mappers that moves data between objects and a database while keeping them independent of each other.

Wikipedia says

A Data Mapper is a Data Access Layer that performs bidirectional transfer of data between a persistent data store (often a relational database) and an in-memory data representation (the domain layer). The goal of the pattern is to keep the in-memory representation and the persistent data store independent of each other and the data mapper itself. This is useful when one needs to model and enforce strict business processes on the data in the domain layer that do not map neatly to the persistent data store.

Programmatic Example

The Data Mapper is a design pattern that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. This pattern promotes the Single Responsibility Principleopen in new window and Separation of Concernsopen in new window.

In the data-mapper module, the pattern is demonstrated using a Student class and a StudentDataMapper interface.

The Student class is a simple POJO (Plain Old Java Object) that represents a student. It has properties like studentId, name, and grade.

public class Student {
    private int studentId;
    private String name;
    private char grade;
    // ...
}

The StudentDataMapper interface defines the operations that can be performed on Student objects. These operations include insert, update, delete, and find.

public interface StudentDataMapper {
    void insert(final Student student);

    void update(final Student student);

    void delete(final Student student);

    Optional<Student> find(final int studentId);
    // ...
}

The StudentDataMapperImpl class implements the StudentDataMapper interface. It contains the actual logic for interacting with the database.

public class StudentDataMapperImpl implements StudentDataMapper {
    // ...
    @Override
    public void insert(final Student student) {
        // Insert student into the database
    }

    @Override
    public void update(final Student student) {
        // Update student in the database
    }

    @Override
    public void delete(final Student student) {
        // Delete student from the database
    }

    @Override
    public Optional<Student> find(final int studentId) {
        // Find student in the database
    }
    // ...
}

The App class contains the main method that demonstrates the use of the StudentDataMapper. It creates a Student object, inserts it into the database, finds it, updates it, and finally deletes it.

public class App {
    public static void main(final String... args) {
        final var mapper = new StudentDataMapperImpl();
        var student = new Student(1, "Adam", 'A');
        mapper.insert(student);
        final var studentToBeFound = mapper.find(student.getStudentId());
        student = new Student(student.getStudentId(), "AdamUpdated", 'A');
        mapper.update(student);
        mapper.delete(student);
    }
}

Program output:

13:54:29.234 [main] DEBUG com.iluwatar.datamapper.App -- App.main(), student : Student(studentId=1, name=Adam, grade=A), is inserted
13:54:29.237 [main] DEBUG com.iluwatar.datamapper.App -- App.main(), student : Optional[Student(studentId=1, name=Adam, grade=A)], is searched
13:54:29.237 [main] DEBUG com.iluwatar.datamapper.App -- App.main(), student : Student(studentId=1, name=AdamUpdated, grade=A), is updated
13:54:29.238 [main] DEBUG com.iluwatar.datamapper.App -- App.main(), student : Student(studentId=1, name=AdamUpdated, grade=A), is going to be deleted

Class diagram

alt text
Data Mapper

Applicability

Use the Data Mapper in any of the following situations

Tutorials

Known uses

  • ORM frameworks such as Hibernate in Java.
  • Data access layers in enterprise applications where business logic and database management are kept separate.
  • Applications requiring database interactions without tying the code to a specific database implementation.
  • SqlSession.getMapper()open in new window

Consequences

Benefits:

  • Promotes Single Responsibility Principleopen in new window by separating persistence logic from business logic.
  • Enhances maintainability and readability by centralizing data interaction logic.
  • Improves application's ability to adapt to changes in the database schema with minimal changes to the business logic.

Trade-offs:

  • Introduces complexity through the additional abstraction layer.
  • Might lead to performance overhead due to the abstraction layer, especially in large-scale applications or with complex queries.
  • Requires developers to learn and understand the abstraction layer in addition to the database and ORM framework being used.
  • Active Record: Combines data access logic and business logic in the domain entities themselves, contrary to Data Mapper's separation of concerns.
  • Object–Relational Mapping (ORM): A technique to map object-oriented programming language data to a relational database.
  • Repositoryopen in new window: Provides an abstraction of the data layer, acting as a collection of domain objects in memory.
  • Unit of Workopen in new window: Manages transactions and keeps track of the objects affected by a business transaction to ensure changes are consistent and transactional.

Credits