首页 > 代码库 > 对象存在性检测集中管理
对象存在性检测集中管理
在大型业务系统中, 常常需要从数据库中查询某个实体对象。 在进行处理之前, 必须先检测该实体是否存在,以增强系统的健壮性。 不过, 检测代码充斥在主业务流程中又会大大降低业务逻辑的清晰性, 最好集中起来进行管理。 因此,编写了一个类来做这个事情, 只需要一行调用就可以完成相应实体对象的检测。
美中不足的一点是, 打 log 的位置发生变化了。由于打 log 的主要目标是为了便于调试和定位错误位置, 为了补偿, 一种办法是调用方法时根据具体语境传入自定义的错误消息, 根据错误消息可以定位错误位置; 另一种办法是, 如果不希望每次调用都使用不同的自定义错误消息, 使用 log.error("error", bizEx) 打印异常栈信息, 也可以获取到抛出错误的位置。
NOTE: 为了遵循公司代码的保密性, 隐去导入的包名, 各位读者可以借鉴其思想, 运用到自己的项目中去。
使用方法:
import static util.CheckEntityExistManager.checkEntity;import util.CheckEntityExistManager.EntityChecker;public class EntityExistCheckTester { private static final Log log = LogFactory.getLog(EntityExistCheckTester.class); public static void main(String[] args) throws BizException { demo(); demo2(); } public static ResultSet demo() { try { // Not Need type casting DeviceDO device = checkEntity(EntityChecker.DISK_ID_CHECK, "d-xxx"); System.out.println(device); VmDO vm = checkEntity(EntityChecker.VM_NAME_CHECK, "vm-name","user-defined msg: vm not exist when creating"); System.out.println(vm); return new ResultSet(IErrorCode.SUCCESSFUL); } catch (BizException bizEx) { log.error("error", bizEx); return new ResultSet(bizEx.getErrorCode()); } catch (Exception ex) { log.error(ex); return new ResultSet(IErrorCode.FAILED); } } public static ResultSet demo2() { try { ZoneDO zone = checkEntity(EntityChecker.ZONE_ID_CHECK, "z-yy"); System.out.println(zone); return new ResultSet(IErrorCode.SUCCESSFUL); } catch (BizException bizEx) { log.error("error", bizEx); return new ResultSet(bizEx.getErrorCode()); } catch (Exception ex) { log.error(ex); return new ResultSet(IErrorCode.FAILED); } }}
实体存在性集中检测类: CheckEntityExistManager:
public class CheckEntityExistManager { private static final Log log = LogFactory.getLog(CheckEntityExistManager.class); private static CheckEntityExistManager checkEntityMgr = getInstance(); private CheckEntityExistManager() { } private static CheckEntityExistManager getInstance() { if (checkEntityMgr == null) { checkEntityMgr = new CheckEntityExistManager(); } return checkEntityMgr; } interface ICheckEntityExist<T> { T checkEntityExist(String entityValue, String msg) throws BizException; } private static ICheckEntityExist<DeviceDO> diskIdChecker = checkEntityMgr.new CheckDiskIdExist(); private static ICheckEntityExist<VmDO> vmNameChecker = checkEntityMgr.new CheckVmExist(); private static ICheckEntityExist<ZoneDO> zoneChecker = checkEntityMgr.new CheckZoneExist(); public enum EntityChecker { DISK_ID_CHECK(diskIdChecker, DeviceErrorCode.DISK_NOT_EXIST), VM_NAME_CHECK(vmNameChecker, VmErrorCode.ERROR_VM_NOT_EXIST), ZONE_ID_CHECK(zoneChecker, ZoneErrorCode.ZONE_NOT_EXIST); EntityChecker(ICheckEntityExist<?> entityChecker, IErrorCode errorCode) { this.entityChecker = entityChecker; this.errorCode = errorCode; } private static Map<String, IErrorCode> errorMap = new HashMap<String, IErrorCode>(); static { for (EntityChecker entityChecker: EntityChecker.values()) { ICheckEntityExist<?> checker = entityChecker.getEntityChecker(); errorMap.put(checker.getClass().getSimpleName(), entityChecker.getErrorCode()); } } private static IErrorCode getErrorCode(String checkClassSimpleName) { return errorMap.get(checkClassSimpleName); } private ICheckEntityExist<?> entityChecker; private IErrorCode errorCode; public ICheckEntityExist<?> getEntityChecker() { return entityChecker; } public void setEntityChecker(ICheckEntityExist<?> entityChecker) { this.entityChecker = entityChecker; } public IErrorCode getErrorCode() { return errorCode; } public void setErrorCode(IErrorCode errorCode) { this.errorCode = errorCode; } } @Autowired private DeviceService deviceService; @Autowired private VmService vmService; @Autowired private ZoneService zoneService; class CheckDiskIdExist implements ICheckEntityExist<DeviceDO> { @Override public DeviceDO checkEntityExist(String diskId, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckDiskIdExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //DeviceDO dev = deviceService.getDevice(diskId); DeviceDO dev = new DeviceDO(); return CheckEntityExistManager.throwexOrReturn(dev, message, errorCode); } } class CheckVmExist implements ICheckEntityExist<VmDO> { @Override public VmDO checkEntityExist(String vmName, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckVmExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //VmDO vm = vmService.queryVmByName(vmName); VmDO vm = null; return CheckEntityExistManager.throwexOrReturn(vm, message, errorCode); } } class CheckZoneExist implements ICheckEntityExist<ZoneDO> { @Override public ZoneDO checkEntityExist(String zoneId, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckZoneExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //ZoneDO zone = zoneService.queryZoneById(zoneId); ZoneDO zone = null; return CheckEntityExistManager.throwexOrReturn(zone, message, errorCode); } } public static <T> T throwexOrReturn(T entity, String message, IErrorCode errorCode) throws BizException { if (entity == null) { log.error(message); throw new BizException(errorCode); } return entity; } public static <T> T checkEntity(EntityChecker entityChecker, String entityValue, String customMsg) throws BizException { ICheckEntityExist<T> checker = (ICheckEntityExist<T>) entityChecker.getEntityChecker(); return checker.checkEntityExist(entityValue, customMsg); } public static <T> T checkEntity(EntityChecker entityChecker, String entityValue) throws BizException { ICheckEntityExist<T> checker = (ICheckEntityExist<T>) entityChecker.getEntityChecker(); return checker.checkEntityExist(entityValue, null); } }
对象存在性检测集中管理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。