Skip to content

fajrbahr/mediatorK

Repository files navigation

MediatorK

Docs Maven Central Kotlin Gradle Android iOS Linux Windows Web (JS/WASM) CI License: CC0

A coroutine-first Mediator library for Kotlin. Implements the CQRS and Vertical Slice patterns — requests go to exactly one handler, notifications fan out to many, and a pipeline of behaviors sits in between.

Full documentation


Installation

dependencies {
    implementation("io.github.fajrbahr:mediatork:0.6.3")
}

For KMP, Maven, and other project types — see Installation.


Quick Start

1 — Define a Request

data class CreateOrderCommand(val id: String, val amount: Double) : Request<Order>

2 — Implement a Handler

class CreateOrderHandler(private val db: OrderRepository) : RequestHandler<CreateOrderCommand, Order> {
    override suspend fun handle(
        mediator: Mediator,
        requestContext: RequestContext,
        request: CreateOrderCommand,
    ): Order {
        val order = Order(request.id, request.amount)
        db.save(order)
        mediator.publish(OrderCreatedEvent(order.id))
        return order
    }
}

3 — Define a Notification

data class OrderCreatedEvent(val orderId: String) : Notification

4 — Implement Notification Handlers

class SendConfirmationEmailHandler : NotificationHandler<OrderCreatedEvent> {
    override suspend fun handle(notification: OrderCreatedEvent) {
        emailService.send(notification.orderId)
    }
}

5 — Register Handlers

class OrderRegistrar(private val db: OrderRepository) : MediatorRegistrar {
    override fun register(registry: HandlerRegistry) {
        registry.scope {
            +CreateOrderHandler(db)
            +SendConfirmationEmailHandler()
        }
    }
}

6 — Create the Mediator

val mediator = MediatorFactory.create(
    registrars = listOf(OrderRegistrar(db)),
)

7 — Use It

val order = mediator.send(CreateOrderCommand("ORD-1", 150.0))

Acknowledgements

First and above all — الحمد لله (Alhamdulillah). This library was built during a hard time, and every line was written with Allah's help and guidance.

Jimmy Bogard — for his talks on Vertical Slice Architecture and MediatR (.NET), which were the direct inspiration for bringing this pattern to Kotlin.

beno.com — the production environment that shaped this library. Real-world usage at scale drove every design decision here.

Ahmed Akilan, Jacqueline Lim, and Jaewoong Eum (skydoves) — Ahmed, our CTO, whose technical mentorship and trust made it possible to grow as an engineer and ship something worth sharing. Jacqueline, whose support and collaboration were invaluable throughout this journey. And Jaewoong — a one-man engineering force whose open-source contributions to the Android community are worth a team of 100 engineers.

Philipp Lackner — for his Android and Kotlin content on YouTube, which has been an invaluable learning resource.

Dr. Venkat Subramaniam — for his exceptional teaching of Kotlin, functional programming, and software design. His talks and courses shaped the way this library thinks about clean code.

droidcon — for the talks, conferences, and community that keep Android and Kotlin engineering moving forward.


Released under CC0 1.0 Universal — public domain. No attribution required.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors