簡體   English   中英

無法重構方法中的代碼?

[英]Not able to refactor the code in methods?

我有一種情況,我有四種方法,其中除了try塊中的一兩行外,包含所有相同的代碼。 有什么建議可以重構嗎?

我嘗試了Enum和switch,但是找不到正確的實現。 問題在於所有方法的返回類型都不同。

 public static void insertDocument(Test database, Test2 collectionName, Document docToInsert) {
        int count = 0;
        int maxTries = getMongoQueryRetries();
        while (count < maxTries + 1) {
            try {
                addUniqueId(docToInsert);
                database.getCollection(collectionName).insertOne(docToInsert);
                return;
            } catch (MongoTimeoutException | MongoSocketReadTimeoutException | MongoSocketClosedException | MongoSocketReadException | MongoNotPrimaryException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (MongoSocketException | MongoServerException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (Throwable e) {
                LOG.error(e.getMessage());
                throw e;
            }

        }

    }


    public static FindIterable<Document> findDocument(MongoDatabase database, String collectionName, Document docToFind) {
        int count = 0;
        int maxTries = getMongoQueryRetries();
        FindIterable<Document> document = null;
        while (count < maxTries + 1) {
            try {
                return database.getCollection(collectionName).find(docToFind);
            } catch (MongoTimeoutException | MongoSocketReadTimeoutException | MongoSocketClosedException | MongoSocketReadException | MongoNotPrimaryException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (MongoSocketException | MongoServerException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (Throwable e) {
                LOG.error(e.getMessage());
                throw e;
            }
        }
        return document;
    }


    public static DeleteResult deleteDocument(MongoDatabase database, String collectionName, Document docToDelete) {
        int count = 0;
        int maxTries = getMongoQueryRetries();
        DeleteResult deleteResult = null;
        while (count < maxTries + 1) {
            try {
                deleteResult = database.getCollection(collectionName).deleteOne(docToDelete);
                return deleteResult;
            } catch (MongoTimeoutException | MongoSocketReadTimeoutException | MongoSocketClosedException | MongoSocketReadException | MongoNotPrimaryException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (MongoSocketException | MongoServerException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (Throwable e) {
                LOG.error(e.getMessage());
                throw e;
            }

        }

        return deleteResult;
    }


    public static void findAndReplaceDocument(MongoDatabase database, String collectionName, Document docToBeReplaced, Document newDocument) {
        int count = 0;
        int maxTries = getMongoQueryRetries();
        while (count < maxTries + 1) {
            try {
                database.getCollection(collectionName).findOneAndReplace(docToBeReplaced, newDocument);
                return;
            } catch (MongoTimeoutException | MongoSocketReadTimeoutException | MongoSocketClosedException | MongoSocketReadException | MongoNotPrimaryException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (MongoSocketException | MongoServerException e) {
                if (++count == maxTries) {
                    LOG.error(e.getMessage());
                    throw e;
                }
            } catch (Throwable e) {
                LOG.error(e.getMessage());
                throw e;
            }

        }
    }

例外:將所有邏輯放在一個地方

我認為可以通過Callable來完成,例如:

public static <T> T processDbAction(Callable<T> dbAction) {
    int count = 0;
    int maxTries = getMongoQueryRetries();
    while (count < maxTries + 1) {
        try {
           return dbAction.call();
        } catch (MongoTimeoutException | MongoSocketReadTimeoutException | MongoSocketClosedException | MongoSocketReadException | MongoNotPrimaryException e) {
            if (++count == maxTries) {
                LOG.error(e.getMessage());
                throw e;
            }
        } catch (MongoSocketException | MongoServerException e) {
            if (++count == maxTries) {
                LOG.error(e.getMessage());
                throw e;
            }
        } catch (Throwable e) {
            LOG.error(e.getMessage());
            throw e;
        }
    }
}

換句話說:您從這樣一個幫助器方法開始,該方法將所有常見的“框架”置於您的呼叫周圍。 然后,您可以擁有Callable接口的不同實現,這些實現可以專注於其核心業務,而不必擔心重試或異常處理/日志記錄。

喜歡:

public FindAction implements Callable<Document> {
   private final MongoDatabase database;
   private final collectionName;
   private final Document docToFind;
... 

 @Override
 public Document call() throws Exception {
   return database.getCollection(collectionName).find(docToFind);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM