Ktor Series : Repository Design Pattern using Ktor and Exposed

Vishwas Trivedi
5 min readDec 7, 2023

Software developers are no strangers to design patterns to make things handy or to complicate things for the next developer.

Jokes aside, today we will be taking look at how we can implement one of those design pattern that helps us to make our life easier : Repository Design Pattern. Repository design pattern is not a new design pattern but the language and framework that we are using today are relatively new.

  • Ktor is a newly introduced asynchronous framework for creating microservices, web applications and more. Written in Kotlin from the ground up.
  • Exposed is a lightweight SQL library on top of JDBC driver for Kotlin language. Exposed has two flavors of database access: type-safe SQL wrapping DSL and lightweight Data Access Objects (DAO).

Let’s not waste any more time, and jump straight into creating the project structure.

API project Structure

Let’s take a look at how we are going to structure out project in respect to repository design pattern. We are going to create API project as are are talking about Ktor and Exposed this time. Following is the high level directory structure of the backend applications.
(Sample repo. link is available at the end of the article)

└── root
├── src
│ └── main
│ ├── kotlin
│ │ └── com.example // Project package name
│ │ ├── config
│ │ ├── controllers
│ │ ├── data
│ │ │ ├── dto
│ │ │ └── entities
│ │ ├── repositories
│ │ ├── services
│ │ └── Application.kt
│ └── resources
│ ├── application.yml
│ └── logback.xml
└── test
└── kotlin
└── com.example
└── ApplicationTest

Now, let take a look at each package : What, why and where we plan to use these packages.

The root directory represents your entire solution with only 1 main folder called src , it mainly has 2 sub directories called mainand test.
As the name suggests main directory contains actual code to be deployed to get Ktor application working while…

--

--

Vishwas Trivedi

I am an accomplished Software Engineer/ Project Manager with 8.5 years of technical experience in driving successful software development teams.