Skip to content

Instantly share code, notes, and snippets.

@notyy
Last active December 27, 2015 07:29
Show Gist options
  • Save notyy/7289426 to your computer and use it in GitHub Desktop.
Save notyy/7289426 to your computer and use it in GitHub Desktop.
//database model
case class Account(id: Identifier, owner: User, var balance: Money)
object AccountRepository {
def lookUp(id: Int):Account = Account("id1", "notyy", 100.0) //just an example
}
//DCI model
class AccountHolder(val account: Account) //adaptor
trait TransferSource { this: AccountHolder =>
def transferOut(amount: Money) = this.account.balance -= amount
}
trait TransferTarget { this: AccountHolder =>
def transferIn(amount: Money) = this.account.balance += amount
}
object TransferService {
def transfer(srcAccount: TransferSource, targetAccount: TransferTarget, amount: Money):Unit = {
if(!canTransfer()) throw new IllegalArgumentException("can't transfer")
srcAccount.transferOut(amount)
targetAccount.transferIn(amount)
}
def canTransfer() = true
}
//context
val transferSource = new AccountHolder(AccountRepository.lookUp(1)) with TransferSource
val transferTarget = new AccountHolder(AccountRepository.lookUp(2)) with TransferTarget
TransferService.transfer(transferSource, transferTarget, 200.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment