Hi,
I’m using command validation in custom interceptor which extends BeanValidationInterceptor (for now I’m using AsynchronousCommandBus but perhaps I’ll switch to jgroups).
For validation I’m calling other services with rest from within this interceptor. In early stage of development I was using spring’s repository to perform direct query to database, but this tights my services together. With rest, services are separated but that takes more time. How are you doing validation and what are the best practices to do so.
I distinguish between two types of validation: structural and business logic validation.
Structural validation is typically done based on the command itself, without the need of any external information. You can do this in a dispatch interceptor (which is invoked before sending the command out).
Business logic validation involves more information than just the command. You can do this in the @CommandHandler, or using a HandlerInterceptor. The latter allows you to execute the logic without grabbing a ‘lock’ on the aggregate. If your validation takes longer, it better to go for the latter option.
Yes, sure. In the end, the data has to come from somewhere. When possible, always use ‘local’ data. However, there simply are circumstances where this is impossible. Do make sure the query you do is efficient, though. It does add to overall latency…