Skip to content

Instantly share code, notes, and snippets.

@upeter
Created March 22, 2011 19:07
Show Gist options
  • Save upeter/881826 to your computer and use it in GitHub Desktop.
Save upeter/881826 to your computer and use it in GitHub Desktop.
package snippets
object DIExamples {
/*
* http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html
* http://debasishg.blogspot.com/2011/03/pushing-envelope-on-oo-and-functional.html
* http://www.assembla.com/wiki/show/liftweb/Dependency_Injection
*/
case class Account(id:String, balance:Double)
def ??? = throw new Error("not implemented yet")
trait Dao[T] {
def findById(id:String):T
def save(t:T)
}
trait AccountService {
def findAccountById(id:String):Account
/**
* Check for sufficient balance and perform transfer
*/
def transfer(from:Account, to:Account, amount:Double):(Account, Account)
}
class Client {
val accountService:AccountService = ???
def performTransfer() = {
val a1 = accountService.findAccountById("1.A")
val a2 = accountService.findAccountById("2.B")
accountService.transfer(a1, a2, 1000)
}
}
}
@TizianoPerrucci
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment