When adapters inject individual use cases, adding handlers can spread wiring changes across the application. A handler registry centralizes routing: adapters send commands or queries through a bus, which resolves each message type to its handler. The registry belongs to bus assembly infrastructure, not the application core. section 4.5.1 section 6.2.3
A typical registry-based dispatch works as follows:
- At startup, discover handler beans and index them by the message types they accept. section 6.2.3
- At runtime, have the bus look up the incoming message type and delegate to its handler, without rescanning the application context. section 6.2.3
- Fail fast when a command has no registered handler; adding a handler should not require adapter changes or manual routing. section 4.5.1
Typed handler routing
A registry can map PublishArticleCommand to PublishArticleHandler by inspecting the handler bean’s generic type. A new typed handler can then join the routing table when the application context starts. section 9.6.3
Listing 9.35 CommandQueryBusAssembler: The bus beans and their registries
@Configuration
class CommandQueryBusAssembler {
@Bean
public CommandBus commandBus(
final ApplicationContext applicationContext) {
return new SpringCommandBus(
new com.emedina.command.spring
.Registry(applicationContext));
}
@Bean
public QueryBus queryBus(
final ApplicationContext applicationContext) {
return new SpringQueryBus(
new com.emedina.query.spring
.Registry(applicationContext));
}
}
The assembler listing shows command and query buses constructed with registries backed by the application context. section 9.6.3
