Skip to content

Instantly share code, notes, and snippets.

@wfaler
Created October 22, 2012 16:57
Show Gist options
  • Save wfaler/3932552 to your computer and use it in GitHub Desktop.
Save wfaler/3932552 to your computer and use it in GitHub Desktop.
simple-cake-pattern-example.scala
// 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
}
@pisfly
Copy link

pisfly commented Jun 11, 2016

Great gist! I have one question, how do you handle connection borrowing and releasing with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment