私はサービス層で行われた "ビジネス検証"を扱う際に質問があります。以下のコードは、十分な資金があることを検証する典型的な口座資金移転の例を示しています。振込額は定義された限度額よりも小さいです。strutsアクションクラスとビジネスサービス層
この例では、呼び出し側はActionクラスで定義された例外を処理して捕捉し、対応するActionErrorを使用してエラーメッセージを表示する必要があります。
すべてのビジネス検証に例外を使用する必要がありますか?
これを例外として使用しないと決めた場合、特定の意味でビジネスレイヤー(対応/結合性に違反する)に対応するActionErrorを定義する必要があります。
サービスレイヤによって伝播されるメッセージをActionクラスに戻す方法を教えてください。
public void transfer(String fromAccount, String toAccount, double amount) throws InsufficientFundsException, TransferLimitException, FactoryException {
try {
Account from = getAccountHome().findByPrimaryKey(
new AccountKey(fromAccount));
Account to = getAccountHome().findByPrimaryKey(
new AccountKey(toAccount));
if (from.getBalance() < amount)
throw new InsufficientFundsException(); // Add action errors
if (from.getTransferLimit() > amount)
throw new TransferLimitException(); // Add action errors
to.deposit(amount);
from.withdraw(amount);
} catch (Exception e) {
throw new FactoryException(
"cannot perform transfer. Nested exception is " + e);
}
}
私の答えを超えて、あなたのコードについて素早く観察してください。 [あなたが精度を失うので、財務オペレーションに 'double'を使うべきではありません。このタイプのトランザクションには、BigDecimalがより適しています(http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)。 – Bogdan