Created
October 22, 2012 16:57
-
-
Save wfaler/3932552 to your computer and use it in GitHub Desktop.
simple-cake-pattern-example.scala
This file contains 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
// 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
Great gist! I have one question, how do you handle connection borrowing and releasing with this?