Skip to content

Commit 8b3e956

Browse files
authored
prioritize the command handler that is related to command before searching for inheritance fixes: #440 (#441)
1 parent eff4eaa commit 8b3e956

File tree

8 files changed

+70
-6
lines changed

8 files changed

+70
-6
lines changed

projects/kediatr-core/src/main/kotlin/com/trendyol/kediatr/RegistryImpl.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ class RegistryImpl(
88
private val registry = Container(dependencyProvider)
99

1010
override fun <TCommand : Command> resolveCommandHandler(classOfCommand: Class<TCommand>): CommandHandler<TCommand> {
11-
val handler = registry.commandMap[baseClassOrItself(classOfCommand, Command::class.java)]?.get()
11+
val handler = registry.commandMap[classOfCommand]?.get()
12+
?: registry.commandMap[baseClassOrItself(classOfCommand, Command::class.java)]?.get()
1213
?: throw HandlerNotFoundException("handler could not be found for ${classOfCommand.name}")
1314
return handler as CommandHandler<TCommand>
1415
}
1516

1617
override fun <TCommand : CommandWithResult<TResult>, TResult> resolveCommandWithResultHandler(
1718
classOfCommand: Class<TCommand>
1819
): CommandWithResultHandler<TCommand, TResult> {
19-
val handler = registry.commandWithResultMap[baseClassOrItself(classOfCommand, CommandWithResult::class.java)]?.get()
20+
val handler = registry.commandWithResultMap[classOfCommand]?.get()
21+
?: registry.commandWithResultMap[baseClassOrItself(classOfCommand, CommandWithResult::class.java)]?.get()
2022
?: throw HandlerNotFoundException("handler could not be found for ${classOfCommand.name}")
2123
return handler as CommandWithResultHandler<TCommand, TResult>
2224
}
@@ -28,7 +30,8 @@ class RegistryImpl(
2830
.flatMap { (_, v) -> v.map { it.get() as NotificationHandler<TNotification> } }
2931

3032
override fun <TQuery : Query<TResult>, TResult> resolveQueryHandler(classOfQuery: Class<TQuery>): QueryHandler<TQuery, TResult> {
31-
val handler = registry.queryMap[baseClassOrItself(classOfQuery, Query::class.java)]?.get()
33+
val handler = registry.queryMap[classOfQuery]?.get()
34+
?: registry.queryMap[baseClassOrItself(classOfQuery, Query::class.java)]?.get()
3235
?: throw HandlerNotFoundException("handler could not be found for ${classOfQuery.name}")
3336
return handler as QueryHandler<TQuery, TResult>
3437
}

projects/kediatr-core/src/test/kotlin/com/trendyol/kediatr/MediatorTests.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class MediatorTests : MediatorUseCases() {
4141
NotificationHandlerThatPassesThroughOrderedPipelineBehaviours(),
4242
TestCommandBaseHandler(),
4343
TestQueryBaseHandler(),
44-
TestCommandWithResultBaseHandler()
44+
TestCommandWithResultBaseHandler(),
45+
TestCommandForInheritanceWithFallbackHandlerHandler(),
46+
TestCommandHandlerForCommandInherited2()
4547
)
4648
)
4749

projects/kediatr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/MediatorUseCases.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ abstract class MediatorUseCases : MediatorTestConvention() {
9696
command2.invocationCount() shouldBe 1
9797
}
9898

99+
@Test
100+
fun inherited_command_with_fallback_gives_priority_to_its_handlers_otherwise_to_fallback_handler() = runTest {
101+
val command = TestCommandForInheritanceWithFallback.TestCommandInherited1("id")
102+
testMediator.send(command)
103+
command.invocationCount() shouldBe 1
104+
command.whereItWasInvokedFrom() shouldBe TestCommandForInheritanceWithFallbackHandlerHandler::class.java.name
105+
106+
val command2 = TestCommandForInheritanceWithFallback.TestCommandInherited2("id")
107+
testMediator.send(command2)
108+
command2.invocationCount() shouldBe 1
109+
command2.whereItWasInvokedFrom() shouldBe TestCommandHandlerForCommandInherited2::class.java.name
110+
}
111+
99112
@Test
100113
fun inherited_query_should_work() = runTest {
101114
val query = TestQueryBase.TestQueryInherited1("id")

projects/kediatr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/models.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ abstract class EnrichedWithMetadata {
1717

1818
fun invocationCount(): Int = getMetadata(INVOCATION_COUNT) as? Int ?: 0
1919

20+
fun whereItWasInvokedFrom(): String = getMetadata("invokedFrom") as? String ?: "unknown"
21+
22+
fun invokedFrom(nameOfTheHandler: String) {
23+
addMetadata("invokedFrom", nameOfTheHandler)
24+
}
25+
2026
internal fun visitedPipeline(pipeline: String) {
2127
val visitedPipelines = visitedPipelines().toMutableSet()
2228
visitedPipelines.add(pipeline)
@@ -554,3 +560,31 @@ class TestCommandWithResultBaseHandler : CommandWithResultHandler<TestCommandWit
554560
return "${command.id} handled"
555561
}
556562
}
563+
564+
sealed class TestCommandForInheritanceWithFallback :
565+
EnrichedWithMetadata(),
566+
Command {
567+
abstract val id: String
568+
569+
data class TestCommandInherited1(
570+
override val id: String
571+
) : TestCommandForInheritanceWithFallback()
572+
573+
data class TestCommandInherited2(
574+
override val id: String
575+
) : TestCommandForInheritanceWithFallback()
576+
}
577+
578+
class TestCommandForInheritanceWithFallbackHandlerHandler : CommandHandler<TestCommandForInheritanceWithFallback> {
579+
override suspend fun handle(command: TestCommandForInheritanceWithFallback) {
580+
command.incrementInvocationCount()
581+
command.invokedFrom(javaClass.name)
582+
}
583+
}
584+
585+
class TestCommandHandlerForCommandInherited2 : CommandHandler<TestCommandForInheritanceWithFallback.TestCommandInherited2> {
586+
override suspend fun handle(command: TestCommandForInheritanceWithFallback.TestCommandInherited2) {
587+
command.incrementInvocationCount()
588+
command.invokedFrom(javaClass.name)
589+
}
590+
}

projects/kediatr-koin-starter/src/test/kotlin/com/trendyol/kediatr/koin/MediatorTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class MediatorTests :
5555
single { TestCommandBaseHandler() } bind CommandHandler::class
5656
single { TestQueryBaseHandler() } bind QueryHandler::class
5757
single { TestCommandWithResultBaseHandler() } bind CommandWithResultHandler::class
58+
single { TestCommandForInheritanceWithFallbackHandlerHandler() } bind CommandHandler::class
59+
single { TestCommandHandlerForCommandInherited2() } bind CommandHandler::class
5860

5961
// Extra
6062
single<MediatorAccessor> { { get<Mediator>() } }

projects/kediatr-quarkus-starter/src/test/kotlin/com/trendyol/kediatr/quarkus/MediatorTests.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,10 @@ class MediatorTests : MediatorUseCases() {
117117

118118
@Produces
119119
fun handler28() = TestCommandWithResultBaseHandler()
120+
121+
@Produces
122+
fun handler29() = TestCommandForInheritanceWithFallbackHandlerHandler()
123+
124+
@Produces
125+
fun handler30() = TestCommandHandlerForCommandInherited2()
120126
}

projects/kediatr-spring-boot-2x-starter/src/test/kotlin/com/trendyol/kediatr/spring/MediatorTests.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import org.springframework.context.annotation.*
4444
NotificationHandlerThatPassesThroughOrderedPipelineBehaviours::class,
4545
TestCommandBaseHandler::class,
4646
TestQueryBaseHandler::class,
47-
TestCommandWithResultBaseHandler::class
47+
TestCommandWithResultBaseHandler::class,
48+
TestCommandForInheritanceWithFallbackHandlerHandler::class,
49+
TestCommandHandlerForCommandInherited2::class
4850
]
4951
)
5052
class MediatorTests : MediatorUseCases() {

projects/kediatr-spring-boot-3x-starter/src/test/kotlin/com/trendyol/kediatr/spring/MediatorTests.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import org.springframework.context.annotation.*
4444
NotificationHandlerThatPassesThroughOrderedPipelineBehaviours::class,
4545
TestCommandBaseHandler::class,
4646
TestQueryBaseHandler::class,
47-
TestCommandWithResultBaseHandler::class
47+
TestCommandWithResultBaseHandler::class,
48+
TestCommandForInheritanceWithFallbackHandlerHandler::class,
49+
TestCommandHandlerForCommandInherited2::class
4850
]
4951
)
5052
class MediatorTests : MediatorUseCases() {

0 commit comments

Comments
 (0)