如何优化RabbitMQ队列消息校验?解决SonarLint逻辑运算符限制
改进嵌套空值校验的方案
原代码问题
你当前的代码在基础设施层的RabbitMQ消息监听方法中,对领域对象TransferPayment的多层属性做了空值校验,但逻辑运算符数量超过了SonarLint的限制,同时代码可读性较差。
@RabbitListener(queues = "${rabbitmq.queue}") public void receivedMessage(TransferPayment transferPayment) { log.info("Received message: {}", transferPayment); // TODO: please correct if (transferPayment.getData().getSecurity() != null && transferPayment.getData().getNotification() != null && transferPayment.getData().getTransfer() != null && transferPayment.getData().getDisbursementDestination() != null && transferPayment.getData().getDisbursementDestination().getThirpartyDestination() != null && transferPayment.getData().getCustomer() != null && transferPayment.getData().getCustomer().getIdentification() != null && transferPayment.getData().getLoans() != null) { notificationUseCase.notifyPayment(transferPayment); } }
方案一:利用领域层封装校验(符合清洁架构)
由于TransferPayment属于领域层,根据清洁架构原则,领域层应封装自身的业务规则(包括数据完整性校验)。我们可以在领域层给TransferPayment(或其内部的Data对象)添加校验方法,把分散的空值判断聚合到领域层:
领域层修改(假设getData()返回的是TransferPaymentData类)
// 领域层 - TransferPaymentData.java public class TransferPaymentData { // 原有属性和方法... public boolean isPaymentNotificationValid() { return getSecurity() != null && getNotification() != null && getTransfer() != null && getDisbursementDestination() != null && getDisbursementDestination().getThirpartyDestination() != null && getCustomer() != null && getCustomer().getIdentification() != null && getLoans() != null; } } // 领域层 - TransferPayment.java public class TransferPayment { private TransferPaymentData data; // 原有getter... public boolean isReadyForNotification() { return data != null && data.isPaymentNotificationValid(); } }
基础设施层代码简化
@RabbitListener(queues = "${rabbitmq.queue}") public void receivedMessage(TransferPayment transferPayment) { log.info("Received message: {}", transferPayment); if (transferPayment.isReadyForNotification()) { notificationUseCase.notifyPayment(transferPayment); } }
这种方式完全符合清洁架构:领域层负责自身的数据校验,基础设施层只需要调用领域对象的校验方法,既规避了SonarLint的逻辑运算符限制,又提升了代码的可维护性。
方案二:基础设施层添加辅助校验方法(不修改领域层的临时方案)
如果暂时无法修改领域层,可以在基础设施层的当前类中添加私有辅助方法,把分散的判断拆分:
@RabbitListener(queues = "${rabbitmq.queue}") public void receivedMessage(TransferPayment transferPayment) { log.info("Received message: {}", transferPayment); if (isTransferPaymentValid(transferPayment)) { notificationUseCase.notifyPayment(transferPayment); } } private boolean isTransferPaymentValid(TransferPayment transferPayment) { TransferPaymentData data = transferPayment.getData(); if (data == null) return false; boolean basicFieldsValid = data.getSecurity() != null && data.getNotification() != null && data.getTransfer() != null && data.getLoans() != null; if (!basicFieldsValid) return false; boolean destinationValid = data.getDisbursementDestination() != null && data.getDisbursementDestination().getThirpartyDestination() != null; if (!destinationValid) return false; boolean customerValid = data.getCustomer() != null && data.getCustomer().getIdentification() != null; return customerValid; }
这种方式通过拆分判断逻辑,每个子判断的逻辑运算符数量都不超过5个,满足SonarLint的要求,同时代码可读性比原代码更好。
内容的提问来源于stack exchange,提问作者joan0627
相关产品推荐
相关产品推荐

