私はService
とを持っていController
ます。
サービス内の各メソッドの前提条件。例:
public void doSomething(Parameter para1 , Parameter para2 ...) {
if ( something wrong ) {
throw new RuntimeException1();
}
if ( another thing wrong ) {
throw new RuntimeException2();
}
// continue do something
}
また、Controller レイヤーには 2 つの方法があります。1 つはshowForm()
、ユーザーが入力するフォームを表示する方法です。もう 1 つはdoApplyForm()
、フォームを受け入れて下層を呼び出すものService.doSomething()
です。
以下は疑似コードです (いくつかのBindingResult
,attr.addFlashAttribute
コードを削除しました) :
@Injected Service service;
public String showForm() {
if ( something wrong ) {
throw new RuntimeException1();
}
if ( another thing wrong ) {
throw new RuntimeException2();
}
return "showForm";
}
public String doApplyForm(@Validated Form form) {
try {
service.doSomething(para1 , para2 ...);
return "redirect:/";
} catch (Exception e) {
// error handling
return "redirect:/error";
}
}
うまく機能しますが、満足していません。中には異臭が漂います。
問題は、showForm()
と同じ前提条件を共有する にありController.doSomething()
ます。
Service.doSomething()
今後別の前提条件を追加する場合Controller.showForm()
は、対応する変更を行う必要があります。
このような悪臭をなくすためのデザインパターンやフレームワークはあるのだろうか?
Java8 の機能的なソリューションは大歓迎です。
ありがとう。