Skip to content

Instantly share code, notes, and snippets.

@joescii
Last active March 8, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joescii/687ffe9090498a643ccf to your computer and use it in GitHub Desktop.
Save joescii/687ffe9090498a643ccf to your computer and use it in GitHub Desktop.
Scala dependency injection with traits
package com.joescii
package object code {
val DbThing = new DbThingImpl()
val HttpThing = new HttpThingImpl()
val BizThing = new BizThingImpl(DbThing, HttpThing)
trait DbThing {
def getDbStuff(params):Future[DbStuff]
}
trait HttpThing {
def getHttpStuff(params):Future[HttpStuff]
}
trait BizThing {
def doComplicatedStuff(params):Future[BizStuff]
}
private [code] class DbThingImpl extends DbThing {
override def getDbStuff(params):Future[DbStuff] = ???
}
private [code] class HttpThingImpl extends HttpThing {
override def getHttpStuff(params):Future[HttpStuff] = ???
}
private [code] class BizThingImpl(db:DbThing, http:HttpThing) extends BizThing {
override def doComplicatedStuff(params):Future[BizStuff] =
db.getDbStuff(params).zip(http.getHttpStuff(params)).map {
case (dbStuff, httpStuff) => ???
}
}
}
@ssanj
Copy link

ssanj commented Feb 10, 2016

I was going for something like what @sleepynate did. Just separate out the purecode (f) from the impure side-effecting code (BizThingImpl). That way you can unit test f easily and then integration test BizThingImpl with Wiremock etc.

@pellunutty's solution looks pretty nice. Because M is a Monad you could substitute Future with the Identity Monad for your tests (or any other Monad you want to).

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