We use the cake pattern to abstract Components (DB, Mock) with self-type annotation, that are Injected at the top level call.
In one case class we want to be able to enrich it's behavior by extending common trait. But how to do it if we want this case class to call an abstract component? Refactoring the case class as abstract will remove all the implementation of apply, unapply, copy ... Things that we need to do the mapping between database and model (for example using Slick).
Here is a simplified exemple representing Blocks as a Resource owned by an Account:
trait Resource {
def getAccount: Account
}
case class Account(id: Int)
case class Block(id: Int, name: String) extends Resource {
this: BlockDBComponent =>
override def getAccount: Account = blockDB.getAccount(this)
}
trait BlockDBComponent {
def blockDB: BlockDB
trait BlockDB {
def getAccount(block: Block): Account = {
// Some call to DataBase
}
}
}
Any idea on how to do it without reimplementing all the case class boilerplate? Will it be simpler putting getAccount in a Resource companion object with case class pattern matching?
Aucun commentaire:
Enregistrer un commentaire