Skip to content

Mediator implementation in Kotlin with native coroutine support

License

Notifications You must be signed in to change notification settings

Trendyol/kediatR

Repository files navigation

kediatR codecov

Humus! The kediatr mascot

Mediator implementation in kotlin with native coroutine support. Supports Spring-Boot, Quarkus and Koin dependency providers.

Tip

"kedi" in Turkish means cat 🐱 and kediatR rhymes with the C# equivalent library mediatR :)

Documentation is available at https://trendyol.github.io/kediatR/

Show me the code

class PingCommand : Command // or
class PingQuery : Query<String> // or
class PingNotification : Notification
class PingCommandHandler : CommandHandler<PingCommand> {
  override suspend fun handle(command: PingCommand) {
    println("Pong!")
  }
}
class PingQueryHandler : QueryHandler<PingQuery, String> {
  override suspend fun handle(query: PingQuery): String {
    return "Pong!"
  }
}

class PingNotificationHandler : NotificationHandler<PingNotification> {
  override suspend fun handle(notification: PingNotification) {
    println("Pong!")
  }
}

class MeasurePipelineBehaviour : PipelineBehaviour {

  override val order: Int = 0

  override suspend fun <TRequest, TResponse> handle(
    request: TRequest,
    next: RequestHandlerDelegate<TRequest, TResponse>
  ): TResponse {
    val start = System.currentTimeMillis()
    val response = next(request)
    val end = System.currentTimeMillis()
    println("Request ${request::class.simpleName} took ${end - start} ms")
    return response
  }
}

val mediator = // create mediator instance in-memory or with dependency injection, take a look at the documentation
mediator.send(PingCommand()) // 1..1
mediator.send(PingQuery()) // 1..1
mediator.send(PingNotification()) // 0..N