6. How to Map Dtos to Database Objects and wise versa

  • Status: accepted

6.1. Context and Problem Statement

We need multiple data transfer objects (DTOs) for different purposes.

  • One for the requests

  • One for the internal logic

  • One for the database

  • One for the responses

Normally they need to map from request to core and then back to a response-dto. To save the updates to the database they need to be mapped to the database object. To map all of these, we need a mapper.

6.2. Decision Drivers

  • Have all mapper at one place

  • Clean and readable code

  • Automatically map objects

6.3. Considered Options

  • object-mapper

  • py-automapper

  • map-struct

6.4. Decision Outcome

py-automapper

6.5. Description of Changes

A library was added. And used in the “core/mapper/” folder.

6.5.1. Folder Structure

  • core/mapper/

    • _init_.py: Contains all imports for the mapper

    • general_mapper.py: Contains a helper method which can be used to map objects with automapper

    • mapper.py: Multiple mapper, one for each model

6.5.2. Naming Pattern

The naming pattern for the mapper-methods is as follows: If a dataclass object needs to be mapped to a dto the name would be dataclass_to_dto. If there exists multiple dtos for one dataclass model, the name does not include “dto” anymore. For example for the job_request_dto the mapper name is: dataclass_to_request.