spring-boot-started library to allow easily follow CQS (Command-Query Separation) principle
Create Command by implementing Command interface
public class ExampleCommand implements Command {
private final String textToLog;
// constructor/getters
}
Then create Bean Handler for that Command (by default only one allowed)
@Component
public class ExampleCommandHandler implements CommandHandler<ExampleCommand> {
void handle(ExampleCommand command) {
log.info(command.getTextToLog());
}
}
Inject somewhere BusProvider and call CommandBus to execute the command
@Service
class ExampleService {
private final BusProvider busProvider;
void executeExampleCommand() {
busProvider.getCommandBus().execute(new ExampleCommand("Log this"));
}
}
Create Query by implementing Query interface
public class ExampleQuery implements Query<String> {
private final String textToLowercase;
}
Then create Bean Handler for that Command (by default only one allowed)
@Component
public class ExampleQueryHandler implements QueryHandler<String, ExampleQuery> {
String handle(ExampleQuery query) {
return query.getTextToLowercase().toLowerCase();
}
}
Inject somewhere BusProvider and call CommandBus to execute the command
@Service
class ExampleService {
private final BusProvider busProvider;
String executeTextExampleQuery() {
return busProvider.getQueryBus().execute(new ExampleQuery("Make ThIS LOwerCase"));
}
}
<dependency>
<groupId>com.github.arttom</groupId>
<artifactId>cqs-spring-boot-starter</artifactId>
<version>0.1</version>
</dependency>