Last active
December 27, 2015 07:29
-
-
Save notyy/7289426 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
//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