the industrial

ブログと言うより自分のためのメモ以外の何モノでもないです。でも読んでくださってありがとうございます。

ScalikeJDBCでDDD的な

大したことではないのだけど、便利そうだったのでメモ。

Long値である従業員IDを明示的に型として保持したいニーズがあるとして、こんな case class があったとする。

case class EmployeeId(value: Long)

SQLのWHERE句を組み立てる際、下記の様になる

val where = sqls.eq(Employees.e.employeeId, employeeId.value)

この .value がちょっと煩わしいなと思っていたのだけど、下記のような implicit conversion を定義しておくと

implicit val intParameterBinderFactory: ParameterBinderFactory[EmployeeId] = ParameterBinderFactory{
  id => (stmt, idx) => stmt.setLong(idx, id.value)
}

こんな風に書けた。

val where = sqls.eq(Employees.e.employeeId, employeeId)

あくまでも自分へのメモw