-
-
Save jsilva/75ecf9ebc3da2b047bff 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
| // The "classic" Repository interface | |
| trait BoatRepository { | |
| def find(uid: String): Future[Boat] | |
| } |
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
| // an abstract declaration of DB instance for the repository | |
| trait Database { | |
| protected def db: DB | |
| } |
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
| // 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 |
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
| // 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 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
| // 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