Hi,
I think I know the answer, and it might be opinionated and both could be correct to use while one solution might be better in performance.
Using Kotlin, data class, event sourcing for aggregates.
I usually always model those classes with val/List/Set -> so it is immutable.
For Aggregate I followed the same idea of immutable, meaning that a command is received, will check some business logic, throw or publish an event.
In case of event, then it will be processed by the aggregate, since my internal data in the aggregate is using classes that immutable, I usually call “.copy(…)” and apply necessary changes from the event to the aggregates fields (add new class instance in mutable list, modify some var fields, etc).
`
class MyAggregate {
@AggregateIdentifier
private var myId: MyId? = null // MyId is inline class
private var someField: Int = 0
private val someList1 = mutableListOf()
private lateinit var someDataButNotInList: SomeData
// … omitted constructor, cmds and events
}
data class SomeData(
val quantityTotal: Int,
val quantityUsedByTime: Int,
val someList2: List
)
`
In this example, we see that normal fields are mutable, that list is mutable but not in “SomeData”, etc.
What would be the best to do?
I guess it would be keep "someList1" as mutable list (and val); and make in "SomeData" mutable fields like "```quantityTotal
" and "quantityUsedByTime" (use var); and make "```someList2" as MutableList<String> (keep val).
What are your opinions?
So far I have all my aggregate following this immutable idea but I start to think it was not the best approach as it will introduce a performance impact when "replaying" events to get the aggregate.
If indeed there is a big performance impact then I will change all my aggregates to follow a more mutable approach.
``