Skip to content
0:00 / 0:00

#contextreceivers#kotlin#transactions

Kotlin Context Receivers for Database Transactions

Pairing with Duncan

4.5K subscribers • 15 videos on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • Presented by Duncan
  • Linked in the descriptionKotlin/KEEPdmcg/gilded-rose-tdd
  • May be out of date. The tools, versions or practices it shows have likely moved on.

Advanced. Deep dive for people who know the field.

2K views3 years ago1 recommendation

Programming languagesDatabases and dataKotlinLanguage design and type systemsPostgres

Duncan refactors database repository methods to use experimental Kotlin context receivers for managing transactions.

In our last episode (youtu.be/Madp0WGq2iA) we introduced a repository to store our stock information, implemented in-memory, in a file, and with Postgres via the Exposed library. This left us with the issue of what to do about database transactions, which we would like to be set up by the caller of methods on our repository. In this episode we first refactor our tests so that they can start the correct type of transaction for the different implementations, and then refactor the repository methods to take that transaction as a parameter. Once this is working, we move the parameter to a receiver, so that our methods have both repository and transaction receivers, before finally introducing the experimental context receivers (github.com/Kotlin/KEEP/blob/master/proposals/context-receive…) that were introduced in Kotlin 1.6.20 and have still to be finalised. By the end I think that the experiment should be judged a success - context receivers seem to work very well in this use-case - so we're going to risk adopting the technology while it is still experimental. This is part 63 of an exploration of where a Test Driven Development implementation of the Gilded Rose stock control system might take us in Kotlin. You can see the whole series as a playlist youtube.com/playlist?list=PL1ssMPpyqociJNwykAOB9_KEZVW7BW7m2 and the code on GitHub github.com/dmcg/gilded-rose-tdd If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (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. I have some free time between producing videos and working for team Gilded Rose. If you like these videos I'd like to work with you - please get in touch - duncan@oneeyedmen.com

Why it's here

1 person recommends this

What people say. Recommended as a practical example of using Kotlin context receivers to handle database transactions cleanly.

u/dmcgin r/Kotlin • Recommends

Introducing Kotlin Context Receivers

u/dmcgin r/Kotlin • Mentions

On “Introducing the Reader Monad - functional Kotlin”

In our last episode ([video we looked at using Kotlin context receivers to provide transactions to functions that need them. Uberto Barbini's upcoming book, From Objects to Functions (https://pragprog.com/titles/uboop/from-objects-to-functions/) details a more traditional functional approach to providing context - the Reader Monad. We would like to understand more about this approach, so in this episode we backtrack a little, refactoring code that takes a transaction as a parameter to instead return a RequiresTransaction type, and then refactor that to a Reader monad. This is part 64 of an exploration of where a Test Driven Development implementation of the Gilded Rose stock control syste…

u/dmcgin r/Kotlin • Mentions

On “The "suspend + receivers" style in Kotlin”

Why read when you can watch [video ;-)