Last active
December 31, 2015 00:49
-
-
Save tlync/7910065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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