A modular monolith todo application showing how independent Gradle subprojects contribute kmpkt-di components (SimpleComponent and configurable Component<Options>) and compose compile-time decorators around a core TodoRepository interface.
flowchart TD
Runner["DemoInteractionRunner"] -->|AddTodo / CompleteTodo| CB["CommandBus"]
Runner -->|ListTodos| QB["QueryBus"]
CB --> CmdH["AddTodoHandler / CompleteTodoHandler"]
QB --> ListH["ListTodosHandler"]
CmdH --> F["PrefixCompletedTodoRepository<br/>(features, priority = 30)"]
ListH --> F
F --> T["TracingTodoRepository<br/>(telemetry, priority = 20)"]
T --> A["AuditingTodoRepository<br/>(audit, priority = 10)"]
A --> Repo["InMemoryTodoRepository<br/>(todos)"]
The todos subproject defines the domain model (TodoItem), commands and query (AddTodo, CompleteTodo, ListTodos), handlers, and the TodoRepository port. TodosComponent implements SimpleComponent to bind InMemoryTodoRepository and discover the module's handlers automatically.
class TodosComponent : SimpleComponent {
override fun ContainerBuilder.register() {
service<InMemoryTodoRepository> { bind<TodoRepository>() }
discover("io.kmpkt.example.modulith.todos") {
services<CommandHandler<*>>()
services<QueryHandler<*, *>>()
}
}
}Other subprojects depend only on todos and wrap TodoRepository at compile time using decorates<TodoRepository>(priority = ...). Higher priority numbers sit on the outside of the chain:
audit(priority = 10):AuditingTodoRepositoryrecords everysavecall toAuditLog.telemetry(priority = 20):TracingTodoRepositoryrecords spans inSpanRecordertagged withserviceName.features(priority = 30):PrefixCompletedTodoRepositoryprepends a configured prefix to completed todo titles onsave.
class PrefixCompletedTodoRepository(
private val delegate: TodoRepository,
private val prefix: String,
) : TodoRepository by delegate {
override fun save(item: TodoItem) {
val updated =
if (item.completed && !item.title.startsWith(prefix)) {
item.copy(title = "$prefix${item.title}")
} else {
item
}
delegate.save(updated)
}
}Modules that need configuration implement Component<Options> and provide a ContainerBuilder extension DSL. FeatureFlagsComponent only registers PrefixCompletedTodoRepository when completedPrefix is non-null, and TelemetryComponent lets the application toggle tracing or set serviceName.
class FeatureFlagsComponent : Component<FeatureFlagsOptions> {
override fun defaults(): FeatureFlagsOptions = FeatureFlagsOptions()
override fun ContainerBuilder.register(config: FeatureFlagsOptions) {
val prefix = config.completedPrefix ?: return
service<PrefixCompletedTodoRepository> {
decorates<TodoRepository>(priority = 30)
argument("prefix", literal(prefix))
}
}
}ModulithConfiguration lives in runtime and only sets module options—TodosComponent and AuditComponent are picked up automatically, and KSP generates the full decorator chain. When DemoInteractionRunner adds "Ship modulith" and completes it, ListTodos returns "[DONE] Ship modulith", and both AuditLog and SpanRecorder capture the operation.
class ModulithConfiguration : Configuration<ModulithApp> {
override fun ContainerBuilder.configure() {
telemetry {
serviceName = "todo-modulith"
}
featureFlags {
completedPrefix = "[DONE] "
}
}
}| Module / File | Role |
|---|---|
TodosComponent.kt |
Core todos domain, handlers, InMemoryTodoRepository, and component |
AuditComponent.kt, AuditingTodoRepository.kt |
audit decorator (priority = 10) and AuditLog |
TelemetryComponent.kt, TracingTodoRepository.kt |
telemetry DSL, decorator (priority = 20), and SpanRecorder |
FeatureFlagsComponent.kt, PrefixCompletedTodoRepository.kt |
featureFlags DSL and decorator (priority = 30) |
ModulithConfiguration.kt, DemoInteractionRunner.kt |
Runtime DI configuration and demo runner |
Main.kt, ModulithAppTest.kt |
Application entrypoint and test |
./gradlew :examples:modulith:app:test