kediatR¶
Mediator implementation for Kotlin.
Usage¶
Gradle
kediatR-core kediatR provides two different packages for spring-boot 2x and 3x. You can use the following dependencies according to your spring-boot version. kediatR-koin-starter kediatR-quarkus-starterCommand dispatching¶
import com.trendyol.kediatr.MappingDependencyProvider.Companion.createMediator
fun main() {
val handler = HelloCommandHandler()
val mediator: Mediator = createMediator(handlers = listOf(handler))
mediator.send(HelloCommand("hello"))
}
class HelloCommand(val message: String) : Command
class HelloCommandHandler : CommandHandler<HelloCommand> {
override suspend fun handle(command: HelloCommand) {
println(command.message)
}
}
Query dispatching¶
import com.trendyol.kediatr.MappingDependencyProvider.Companion.createMediator
fun main() {
val handler = GetSomeDataQueryHandler()
val mediator: Mediator = createMediator(handlers = listOf(handler))
val result: String = mediator.send(GetSomeDataQuery(1))
println(result)
}
class GetSomeDataQuery(val id: Int) : Query<String>
class GetSomeDataQueryHandler : QueryHandler<GetSomeDataQuery, String> {
override suspend fun handle(query: GetSomeDataQuery): String {
// you can use properties in the query object to retrieve data from somewhere
// val result = getDataFromSomewhere(query.id)
// return result
return "hello"
}
}
Pipeline Behavior¶
class CommandProcessingPipeline : PipelineBehavior {
override val order: Int = 1
override suspend fun <TRequest, TResponse> handle(
request: TRequest,
next: RequestHandlerDelegate<TRequest, TResponse>
): TResponse {
println("Starting process.")
val result = next(request)
println("Ending process.")
return result
}
}
SpringBoot¶
- Add kediatr-spring dependency to your maven or gradle dependencies
@Service
class UserService(private val mediator: Mediator) {
suspend fun findUser(id: Long) {
return mediator.send(GetUserByIdQuery(id))
}
}
class GetUserByIdQuery(private val id: Long) : Query<UserDto>
@Component
class GetUserByIdQueryHandler(private val userRepository: UserRepository): QueryHandler<GetUserByIdQuery, UserDto> {
override suspend fun handle(query: GetUserByIdQuery): UserDto {
val user = userRepository.findById(query.id)
// do some operation on user
return UserDto(user.id, user.name, user.surname)
}
}
Koin¶
Simply inject KediatR as a singleton dependency with any module and inject handler instances. KediatRKoin.getMediator() must be in the same module with at least one Handler to get correct package name for reflection. Please note that this is an experimental release and reflection strategy with koin is a little wonky. Please open a pull request if you think there is a better implementation.
val kediatRModule = module {
single { KediatRKoin.getMediator() }
single { GetUserByIdQueryHandler(get()) }
}
class UserService(private val mediator: Mediator) {
fun findUser(id: Long) {
return mediator.send(GetUserByIdQuery(id))
}
}
class GetUserByIdQuery(private val id: Long) : Query<UserDto>
class GetUserByIdQueryHandler(private val userRepository: UserRepository) : QueryHandler<GetUserByIdQuery, UserDto> {
fun handle(query: GetUserByIdQuery): UserDto {
val user = userRepository.findById(query.id)
// do some operation on user
return UserDto(user.id, user.name, user.surname)
}
}
Quarkus¶
- Add kediatr-quarkus-starter dependency to your dependencies
- Quarkus does not index 3rd party libraries unless you explicitly indicate. Add this configuration to ** application.properties** file.
class UserService(private val mediator: mediator) {
fun findUser(id: Long) {
return mediator.send(GetUserByIdQuery(id))
}
}
class GetUserByIdQuery(private val id: Long) : Query<UserDto>
@ApplicationScoped
class GetUserByIdQueryHandler(private val userRepository: UserRepository) : QueryHandler<GetUserByIdQuery, UserDto> {
override suspend fun handle(query: GetUserByIdQuery): UserDto {
val user = userRepository.findById(query.id)
// do some operation on user
return UserDto(user.id, user.name, user.surname)
}
}