Skip to content

Instantly share code, notes, and snippets.

@jg
Forked from wfaler/cake-pattern-example.scala
Created July 7, 2013 16:58
Show Gist options
  • Save jg/5944090 to your computer and use it in GitHub Desktop.
Save jg/5944090 to your computer and use it in GitHub Desktop.
// simple example of the cake pattern
// abstract DAO trait
trait Repository[A, B]{
// saves an entity, returns an ID
def save(entity: A): B
// more features..
}
trait RdbmsRepository extends Repository[MyUserCaseClass, Long]{
def save(entity: MyCaseClass): Long = {
//concrete implementation of save
}
}
// service class, note the "self-type" at the top, signifying it "must have a repository mixed in"
trait UserService{
this: Repository[MyUserCaseClass, Long] =>
// by virtue of having a repository mixed in, the repository's functions become available
//without having to know which concrete implementation is mixed in at runtime.
def addUser(user: MyUserCaseClass): Long = save(user)
}
// place to bootstrap dependencies
class Bootstrap{
// using "with" to mixin the dependency. Can use a Fake/Mock trait during test
val userService = UserService with RdbmsRepository
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment