Skip to content

Instantly share code, notes, and snippets.

@orekyuu
Created January 24, 2019 12:48
Show Gist options
  • Save orekyuu/7e4bc44b0ac1a4aed61f7e8e8a36d229 to your computer and use it in GitHub Desktop.
Save orekyuu/7e4bc44b0ac1a4aed61f7e8e8a36d229 to your computer and use it in GitHub Desktop.
ドメインクラスをControllerで受け取りたい

やりたいこと

  • Formからドメインオブジェクトに変換するときにプリミティブな型から値クラスに変換するところでミスりそう
  • ドメインオブジェクトで受け取れたらドメインオブジェクトにバリデーション書けて便利やん?

理想

ドメインオブジェクトとか

class EmployeeName {
  @NotBlank
  final String name;
  
  EmployeeName(String name) {
    this.name = name;
  }
}

class Employee {
  @Valid
  final ID<Employee> id;
  @Valid
  final EmployeeName name;
  
  Employee(ID<Employee> id, EmployeeName name) {
    //...
  }
}
@Controller
class HogeController {

  @PostMapping
  public String create(@Validated Employee employee, BindingResult bindingResult) {
    if(bindingResult.hasError()) {
      // ...
    }
    // ここまできたらemployeeは正しい状態になってるって素敵やん?
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment