App Architecture: How to map data between layers

Mkhytar Mkhoian
ProAndroidDev
Published in
3 min readApr 25, 2024

--

Photo by Joshua Sortino on Unsplash

This is a series of articles about how to architecture your app that it was inspired by Google Guide to App Architecture and my personal experience.

What architecture to choose for the project? A common question for many engineers and often the choice is to follow Clean Architecture principles. Put the code in different layers, usually, it’s Data, Domain, and Presentation. Each of the layers has its components logic, and data models. One of the main ideas of different layers is to have a dependency hierarchy and in the context of that today, I’ll show you how to map data to transfer through the layers.

Let's assume we have the dependency hierarchy as in the picture below and zero in on each layer of data mapping.

Naming conventions

Before we shed the light on each layer, let's agree on naming conventions. The mapping functions are named after the layer data type. We reserve prefixes for specific functions that map data model representation between layers. The convention is as follows:

as + layer data type.

For example, we have CategoryDTO, Category, CategoryModel for Data, Domain, and Presentation layers respectably. The mapping function's names will be asDTO, asDomain, asPresentation respectively.

If you need to transform one type to another we reserve prefix to. The convention is as follows:

to + data type.

For example, CategoryModel has adsCount property that we want to map to UI string representation, and create the toAdsCountFormat function.

The difference between prefixes as and to is that use as for name when we map the same model that has representation in each layer and to for function to map one type to complete a different one.

Data layer

This layer knows about the Domain and is responsible for mapping the Domain model to DTO and DTO to the Domain when we expose data. The DTO model usage should be encapsulated in the Data layer only.

As you can see DTO models are used for parsing JSON and we shouldn't expose this logic out of the Data layer, it allows us to align the DTO model with the server or local cache data structure.

Put mapping functions in the same Kotlin file as the model declaration.

Domain layer

The Domain models shouldn’t contain any layer mapping function, because the Domain doesn’t know about Data and Presentation layers at all. The model on this layer shouldn’t be Parcalable or any other platform-dependent code. This layer is the source of truth in the project and should be platform-independent, so we can reuse it for different platforms (e.g. Kotlin multiplatform project).

Presentation layer

The Presentation layer knows about the Domain but doesn’t know about the Data layer. The logic for mapping the Domain models into Presentation and vice versa should be in the Presentation layer. Models in this layer can be platform-dependent (e.g. Parcelable) and be more UI-specific.

Mapping functions:

DTO to Domain Model mapping test

The mapping logic also needs to be coveted by JUnit tests

Today we have covered a few changes that can add more structure to your project and improve code style.

Stay tuned for the next App Architecture topic to cover.

Thanks for reading.

--

--