Data Transfer Object (DTO) with Kotlin data classes and extension functions

It’s something simple but makes the code much cleaner

Filipe Martins
3 min readJan 18, 2022

Introduction

Today at work, by accident, we created a really cool thread on Slack talking about some interesting functions that some languages have…and I ended up talking about how I like and have been using Kotlin’s extension functions to do the mappings of my DTOs.

Although simple, using extension functions for mapping makes the code cleaner and eliminates the need for mapper classes or libraries in many cases.

That’s what motivated this short article, I hope you like it!

Data Transfer Object (DTO)

"Although the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single call, it’s worth mentioning that another advantage is to encapsulate the serialization mechanism for transferring data over the wire. By encapsulating the serialization like this, the DTOs keep this logic out of the rest of the code and also provide a clear point to change serialization should you wish.

And in Kotlin we create DTOs using data classes:

Kotlin Data classes

It is not unusual to create classes whose main purpose is to hold data. In such classes, some standard functionality and some utility functions are often mechanically derivable from the data. In Kotlin, these are called data classes

And we need to map between DTOs and entities, which can be accomplished with mapper classes, libraries, or extension functions:

Kotlin extension functions

Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.

With the concepts well defined, I want to show the way I’ve been building and converting my DTOs with Kotlin.

In time: This is not the only way, nor the right way… it’s just another way, ok? ; )

Code example

Entity

Repository

Requests DTOs and the respective extension functions to convert to the entity

Response DTO and the respective extension function to convert from the entity

Controller

Note: Calling directly the repository for the sake of simplicity of this example.

The code is self explanatory and in the Controller everything is tied together, making it clear how cool this is:

We have a lot of control over how the information arrives (request), how it is transformed into our entities and then how they are returned (response) in a very clean way!

Thanks for your time and see you on the next article!

Repository branch with code example

Using Spring Web, Spring Data JPA and H2 Database

Cloning and checking out the branch

git clone https://github.com/filipefox/kotlin-spring-boot-demos.gitcd kotlin-spring-boot-demosgit checkout data_transfer_object_with_kotlin_extension_function

Running the project

Open the project with "IntelliJ IDEA", right click on "DemoApplication.kt" and click on "Run DemoApplicationKt".

Postman collection with CRUD operations

  • Open Postman > "Collections" > "Import" > "Link".
  • Where it says "Enter a URL" copy and paste the link below:
https://gist.githubusercontent.com/filipefox/18c5a52868eddf44c42a079f7f3c53ed/raw/7c09a1ee1b0aa67023a25c8e06f9d772fcde72a3/data_transfer_object_with_kotlin_extension_function.postman_collection.json
  • Click "Continue" and finally at "Import".

--

--