Skip to content

Instantly share code, notes, and snippets.

@jsilva
Forked from ubourdon/BoatRepository.scala
Last active August 29, 2015 14:09
Show Gist options
  • Save jsilva/75ecf9ebc3da2b047bff to your computer and use it in GitHub Desktop.
Save jsilva/75ecf9ebc3da2b047bff to your computer and use it in GitHub Desktop.
// The "classic" Repository interface
trait BoatRepository {
def find(uid: String): Future[Boat]
}
// an abstract declaration of DB instance for the repository
trait Database {
protected def db: DB
}
// For example use a MongoDb instance dao for insert Boat
trait MongoDbBoatDao extends BoatRepository with Database {
def find(uid: String) = db.find(uid) // here code the Dao implementation
}
// Here we construct the Scala object Dao using trait mixin
object MongoDbBoatDao extends MongoDbBoatDao with ProdDatabase
// with this example test we can show how overload ProdDatabase instance
class MongoDbBoatDaoTest extends FunSuite {
val connection: DB = {
// create a DB instance test
}
// override db with test db instance to perform testing
trait TestDbInjection extends Database { val db = connection }
object UnderTestDao extends MongoDbBoatDao with TestDbInjection
test("should insert boat") {
UnderTestDao.insert(...)
// test something ...
}
}
// This trait produce a concrete implementation of DB instance to inject into Dao
trait ProdDatabase extends Database {
protected val db = FactoryDb.init() // a example way of retrieve a DB instance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment