https://kotlinlang.org logo
#exposed
Title
# exposed
d

Dominik Szczygieł

10/05/2023, 6:21 AM
Hi guys, Im moving from Java to Kotlin and now I'm learning Exposed and I have problem how use it in my project. I use Spring Framework and don't know what layers should I separate. Especially I was thinking about the repository/persistence layer. Should I create repository layer when using Exposed? If so how service layer should communicate with repository, data classes with same fields as entity but not connected with Exposed? So it would look something like that DTOs <-> model <-> Exposed entities. Or maybe I should use transaction block inside service layer? But what about testing in this case? There are guidelines and best practices for this topic? I'd be grateful for any guidance on this topic 🙂
g

Goetz Markgraf

10/05/2023, 6:35 AM
We are currently using Exposed in a Spring Boot project. We created Classes as @Repository that handle the loading and storing of data via Exposed. Any Exposed code is in there. We use Exposed DSL and not the DAO. “Above” these Repositories no Exposed Code or Entities are used. Most of the time, we use the DTOs in Services, But sometimes we have backend specific data types. Controller <-> Service(s) <-> Repository Only Repositories are allowed to have side effects, all other classes and functions are / must be / pure. Works just fine
👍 1
d

Dominik Szczygieł

10/05/2023, 7:14 AM
Thank you for your answer 🙂 My exact case was something like this - to create a parcel I need multiple entities from different tables, I was using DAO so extracting these entities must be done in the transaction block. Then it seemed that validation and some business logic must be moved to the repository layer, which sounds bad. So in this case, If I use DSL and map results from the database to pure data classes, pass them to the service layer, do some validation and buissness logic and pass them back to the repository layer and also using DSL to pass it to database, will that be the correct way of doing it? So using DAO makes me to pass Exposed entities between repository and service layer?
g

Goetz Markgraf

10/05/2023, 2:57 PM
I don’t think, that there is a “right way”. But I don’t see a great value in the DAO API, because you have to use the DAOs. I want to keep all Exposed Code in the Repository – so that you could change Exposed with something else. Also, in tests, you can mock the repository and have DB-less tests very easily.
🙌 1
d

Dominik Szczygieł

10/05/2023, 4:54 PM
Okay, I understand. Thank you for your help :)
j

Jeremy Luinstra

10/05/2023, 8:12 PM
I've been using this combo for years, and have a similar approach to what @Goetz Markgraf is doing. Including the code isolation! I use data classes as models and write my own repositories using the DSL. I have found to DAO to be not that helpful for most cases more complicated than simple CRUD operations, and its pretty rare I have a service that simple. I also really like the Kotest/Testcontainers combination for testing when I actually do want to test the DB layers. Leave Spring out of that mix as much as you can to keep the tests quick.
🙌 1
👍 1
d

Dominik Szczygieł

10/07/2023, 8:06 PM
Moving from DAO to DSL was the solution, now after rewriting my repository everything looks as I expected, and all Exposed usages are hidden behind the repository. Your advice were really helpful, thanks a lot 🙂
🙌 2