Joram Visser
04/30/2021, 7:57 AMJoram Visser
04/30/2021, 7:58 AMdata class MyDomain(val id: Long, val some: String, val things: String)
@Mapper
interface MyMapper {
fun insert(myDomain: MyDomain)
}
// <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
// INSERT INTO my_domains (some, things) VALUES (#{some}, #{things})
// </insert>
fun somePseudoCode(): MyDomain {
val myDomain = MyDomain(-1L, "test", "123" )
println(myDomain.id) // prints -1
myMapper.insert(myDomain)
println(myDomain.id) // prints for instance 35 (or whatever seq.nextval did produce)
return myDomain
}
Joram Visser
04/30/2021, 8:00 AMephemient
04/30/2021, 8:01 AMephemient
04/30/2021, 8:06 AMval aPair = Pair("a", 1)
val bPair = Pair("b", 1)
with(aPair::class.java.declaredFields.first()) {
isAccessible = true
set(aPair, "b")
}
aPair == bPair
ephemient
04/30/2021, 8:06 AMJoram Visser
04/30/2021, 8:09 AMdave
04/30/2021, 8:09 AMJoram Visser
04/30/2021, 8:11 AMJoram Visser
04/30/2021, 8:22 AMclass MyDomain(var id: Long, var some: String, var things: String)
And/or, would it be a good idea to wrap the insert function in a function that encapsulates the mutability? So that the rest of the program can rely on immutability.Joram Visser
04/30/2021, 8:23 AMdave
04/30/2021, 8:24 AMJoram Visser
04/30/2021, 8:26 AMephemient
04/30/2021, 8:29 AMval
doesn't mean that it doesn't change, just that this isn't the mechanism for doing so. for example,
val x: Int
get() = kotlin.random.Random.nextInt()
of course, your case is a different since it's a data class
, but just to be clearJoram Visser
04/30/2021, 8:35 AMkotlin.random.Random.nextInt()
from your example. It is final, set in stone, the value is assigned and can't be changed.
Maybe a better example would be assigning class MyDomain(var id: Long, var some: String, var things: String)
to a val
. Now it is immutable! You can't change that val to something else. But you can still change the properties of that instance; set the some
to a new value, etc. The things inside your immutable reference are mutable in that case.Joram Visser
04/30/2021, 8:36 AMchristophsturm
04/30/2021, 8:54 AMmyMapper.insert(myDomain.copy())
Joram Visser
04/30/2021, 9:16 AMchristophsturm
04/30/2021, 9:21 AMsavedUser = repo.create(user)
thats a kotlin friendly api imochristophsturm
04/30/2021, 9:22 AMJoram Visser
04/30/2021, 9:28 AMJoram Visser
04/30/2021, 9:34 AMchristophsturm
04/30/2021, 9:41 AMdave
04/30/2021, 9:42 AMJoram Visser
04/30/2021, 9:45 AMJoram Visser
04/30/2021, 9:57 AMchristophsturm
04/30/2021, 10:10 AMthe/a problem is that you can’t tell the difference between a saved and unsaved entity.i found that in practice my code always knows if an entity is persisted or not and in usually don’t need a createOrUpdate method
dave
04/30/2021, 10:15 AMchristophsturm
04/30/2021, 10:23 AMchristophsturm
04/30/2021, 10:25 AMJoram Visser
04/30/2021, 10:29 AMJoram Visser
04/30/2021, 10:31 AMdave
04/30/2021, 10:47 AMinterface Repo {
fun add(new: Item<Nothing?): Item<PK>
fun get(new: PK): Item<PK>?
fun all(): List<Item<PK>>
}
This means that you can't try to insert an existing record, and that you can guarantee that all records with a PK have been saved. It centralises the allocation of the PKs (which won't always be UUIDS). We can also enforce the flow of the data through the system in a typesafe way.dave
04/30/2021, 10:48 AMchristophsturm
04/30/2021, 11:00 AMchristophsturm
04/30/2021, 11:03 AMdave
04/30/2021, 11:03 AMdave
04/30/2021, 11:04 AMchristophsturm
04/30/2021, 11:06 AMchristophsturm
04/30/2021, 11:07 AMdave
04/30/2021, 11:11 AMchristophsturm
04/30/2021, 11:12 AMchristophsturm
04/30/2021, 11:12 AMdave
04/30/2021, 11:14 AMchristophsturm
04/30/2021, 11:33 AMchristophsturm
04/30/2021, 11:34 AMdave
04/30/2021, 11:35 AMchristophsturm
04/30/2021, 11:38 AMdave
04/30/2021, 11:39 AMdave
04/30/2021, 11:40 AMchristophsturm
04/30/2021, 11:40 AMchristophsturm
04/30/2021, 11:42 AMchristophsturm
04/30/2021, 11:42 AM