-
-
Save hanrw/d3f21d0dd309af65c113 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
// 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