It never ceases to amaze me how bad I am at predicting the subject of the next week’s video. This week I really did set out to benchmark Ktor (https://ktor.io/) and http4k ((https://http4k.org) for endpoints that perform IO, but it very quickly became clear that the data model from the example project wasn’t suitable. It uses Kotlin’s MutableList, but the operations on that interface are not suspend functions, and Ktor needs coroutines in order to not block on IO.
So instead of measuring I ended up refactoring, migrating from MutableList to a repository object that is much more amenable to implementation with a database, even if in the tests we still actually hold data in an ArrayList.
In this episode
00:00:44 Our current model is MutableList of Customer
00:02:09 REST is also a model
00:02:49 Fixing the response for an empty list
00:04:46 What is the problem with MutableList?
00:05:40 Start by introducing a typealias for MutableList of Customer
00:07:12 Now make the type alias an interface
00:08:31 IntelliJ import bug
00:10:44 We can't serialize our custom type, but we can sidestep
00:11:58 Implement findById
00:15:00 Implement addCustomer
00:16:08 Implement deleteById
00:17:09 Now make Customers not a List
00:17:21 Lean on the compiler to find issues
00:17:36 Distinguishing between production and test operations
00:20:36 Some final tidying and conveniences
00:22:47 Customers now lets us see the operations we are using
00:23:17 There is a problem with suspend though
00:23:58 that we'll punt into next week
The code for this video is on a GitHub fork https://github.com/dmcg/ktor-document...
This video is in a playlist of Ktor episodes ( • Ktor ) and http4k ( • http4k )
I get lots of questions about the test progress bar. It was written by the inimitable @dmitrykandalov. To use it install his Liveplugin (https://plugins.jetbrains.com/plugin/...) and then this gist https://gist.github.com/dmcg/1f56ac39...
If you like this video, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.
Watch video Refactoring to the Repository Pattern online without registration, duration hours minute second in high quality. This video was added by user Pairing with Duncan 28 June 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,438 once and liked it 65 people.