您好,登錄后才能下訂單哦!
在大型業務系統中, 常常需要從數據庫中查詢某個實體對象。 在進行處理之前, 必須先檢測該實體是否存在,以增強系統的健壯性。 不過, 檢測代碼充斥在主業務流程中又會大大降低業務邏輯的清晰性, 最好集中起來進行管理。 因此,編寫了一個類來做這個事情, 只需要一行調用就可以完成相應實體對象的檢測。
美中不足的一點是, 打 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); } }
其他精彩文章文章
更多關于android開發文章
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。