Skip to content

Instantly share code, notes, and snippets.

@tlync
Last active December 31, 2015 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlync/7910065 to your computer and use it in GitHub Desktop.
Save tlync/7910065 to your computer and use it in GitHub Desktop.
// base (サンプルなのでただの marker trait)
trait Entity
trait Repository
// domain layer
case class User(id: Int, name: String) extends Entity
trait Users extends Repository {
def find(id: Int): Option[User]
}
// infra layer
import scalikejdbc._
import SQLInterpolation._
class ScalikeJdbcBasedUsers extends Users {
private def rsToEntity(rs: WrappedResultSet): User = {
User(rs.int("id"), rs.string("name"))
}
def find(id: Int): Option[User] = {
DB readOnly { implicit session =>
sql"select * from users where $id".map(rsToEntity).single().apply()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment