Skip to content

Instantly share code, notes, and snippets.

@kalys
Created February 5, 2014 12:04
Show Gist options
  • Save kalys/8822301 to your computer and use it in GitHub Desktop.
Save kalys/8822301 to your computer and use it in GitHub Desktop.
// ...
import com.escalatesoft.subcut.inject._
import NewBindingModule._
//...
trait SomeService {
def name:String
}
object RealService extends SomeService {
def name = "real"
}
object FakeService extends SomeService {
def name = "fake"
}
object MySingleton {
def someMethod(someArgument:String)(implicit bindingModule:BindingModule):String = { // implicit argument is passed
val service = bindingModule.inject[SomeService](None) // pay attention to None
val serviceName = service.name
s"This is $serviceName service"
}
}
object SomeConfigurationModule extends NewBindingModule(module =>{
module.bind [SomeService] toSingle RealService
})
// for use on production
MySingleton.someMethod("ololo")(SomeConfigurationModule)
// for testing
SomeConfigurationModule.modifyBindings { implicit bindingModule =>
bindingModule.bind [SomeService] toSingle FakeService
MySingleton.someMethod("argument")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment