Hi folks! My take on how `context parameters` can ...
# feed
o
Hi folks! My take on how
context parameters
can help domain modelling and aggregate root design: https://oguzhansoykan.com/posts/2025/2025-07-21-context-parameters-ddd/
a
but I can still do the following:
Copy code
with (product) {
   variant.changeSizeAndDimensions()
}
o
Yes, that’s what code-reviews for 😛 You can protect entity not only with AR itself in the context, but with protected components from its context.
context(ar: Product, x: ProtectedComponent)
x
is a
protected
component only accessible inside of the AR. Blog post sheds small light to the usage, the rest on people’s imagination 🙂
a
Okay, but code reviews will also solve the original problem then? 😄
o
Okay, but code reviews will also solve the original problem then?
In theory, indeed 😄
On the bright side, if context parameters solve most intuitive mistakes, still a good gain. Secondly, you can create detekt rules for the forbidden patterns, too. Meaning,
with
can’t be used over ARs, etc.
a
IMHO, by just putting the domain in a separate module and using the internal modifiers you can restrict it as well 🙂
Copy code
package domain

data class Product(
    val name: String,
    val price: Double,
) {
    fun variants(): List<Variant> = listOf(Variant("foo"))
}

data class Variant(
    val name: String,
) {
    internal fun doSomething() {
    }
}
And in another gradle module:
Copy code
fun main() {
    val product = Product("Foo", 100.0)
    val variants = product.variants()
    variants.first().doSomething() // wont compile
}
o
That’s also a good approach, indeed
a
I like context parameters, but they are another cognitive overhead. At this moment I feel, since they are quite unique, that the Kotlin community is still figuring out what is possible besides DSL improvements. So these articles definitely help!
o
I am sure we will see lots of other usages. It brings new ways of interacting class structure as my exploration in the article goes. There are several ways of doing thing as always. There is no right or wrong, or worse. If you create DSL without context parameters, it works, too. So, it is up to use-cases. I am really curious also how people use it. So far, not much content.