Created
December 10, 2019 10:41
-
-
Save saswata-dutta/a1457136fe76f7908592fea93914d22d to your computer and use it in GitHub Desktop.
using scala trait and object for DI
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
// https://www.michaelpollmeier.com/2014/06/29/simple-dependency-injection-scala | |
// define | |
trait MyService { | |
def foo = 42 | |
} | |
// instantiate | |
object MyService extends MyService | |
// inject | |
class MyClient { | |
val service: MyService = MyService | |
} | |
// dual implementation | |
trait MyOtherService extends MyService {...} | |
object MyOtherService extends MyOtherService | |
val client = new MyClient { override val service = MyOtherService } | |
// test | |
val mockService = mock[MyService] | |
val testClient = new MyClient { override val service = mockService } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment