Skip to content

Instantly share code, notes, and snippets.

@OlegIlyenko
Last active December 17, 2015 14:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegIlyenko/5623423 to your computer and use it in GitHub Desktop.
Save OlegIlyenko/5623423 to your computer and use it in GitHub Desktop.
import scaldi.{Injectable, Module, Injector}
class UserService
class UserController(implicit inj: Injector) extends Injectable {
val userService = inject [UserService]
}
class OrderService(implicit inj: Injector) extends Injectable {
val userService = inject [UserService]
}
class OrderController(implicit inj: Injector) extends Injectable {
val orderService = inject [OrderService]
}
class UserModule extends Module {
binding to new UserService
binding to new UserController
}
class OrderModule extends Module {
binding to new OrderService
binding to new OrderController
}
implicit val appModule = new UserModule :: new OrderModule
import Injectable._
println(inject[OrderService].userService)
import com.escalatesoft.subcut.inject.{NewBindingModule, BindingModule, Injectable}
class UserService
class UserController(implicit val bindingModule: BindingModule) extends Injectable {
val userService = inject [UserService]
}
class OrderService(implicit val bindingModule: BindingModule) extends Injectable {
val userService = inject [UserService]
}
class OrderController(implicit val bindingModule: BindingModule) extends Injectable {
val orderService = inject [OrderService]
}
class UserModule extends NewBindingModule ({ implicit module =>
import module._
bind [UserService] toSingle new UserService
bind [UserController] toSingle new UserController
})
class OrderModule extends NewBindingModule ({ implicit module =>
import module._
bind [OrderService] toSingle new OrderService
bind [OrderController] toSingle new OrderController
})
val appModule = new UserModule ~ new OrderModule
println(appModule.inject[OrderService](None).userService)
// Explodes with following exception:
// Exception in thread "main" com.escalatesoft.subcut.inject.BindingException: No binding for key BindingKey(UserService,None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment