Share
Embed
Dependency injection in Scala: The sequel - SubCut
A follow up presentation on dependency injection in Scala using SubCut.
Jan Fredrik Wedén
December 01, 2011
Transcript
19 slides
-
Dependency injec+on in Scala The sequel - SubCut javaBin,
Trondheim – 1.12.2011 -
History lesson SubCut (My personal) Crystal Ball
Scala Uniquely Bound Classes Under Traits -
History lesson Constructor injec+on Cake pa?ern new
SomeClass(dep1, dep2) … -
trait SomeComponent { val aThing: SomeInterface trait SomeInterface { //
Stuff } } trait AnotherComponentImpl extends AnotherComponent { this: SomeComponent => def myMethod { aThing.usefulStuff() } } object ComponentRegistry extends AnotherComponentImpl with SomeComponentImpl { val aThing = new MyFavouriteImpl } -
object ProjectConfiguration extends NewBindingModule({ module => import module._ // can
now use bind directly bind [UserRepository] toInstance new RealUserRepository bind [Analyzer] identifiedBy 'webAnalyzer to instanceOfClass [WebAnalyzer] bind [Session] identifiedBy 'currentUser toProvider { WebServerSession.getCurrentUser().getSession() } bind [Int] identifiedBy 'maxThreadPoolSize toInstance 10 bind [WebSearch] toLazyInstance { new GoogleSearchService()(ProjectConfiguration) } ... }) Configura+on module -
Injectable class object ProjectConfiguration extends NewBindingModule({ module => ...
}) class UserService(val serviceId: String, webAnalyzer: Option[Analyzer] = injected) (implicit val bindingModule: BindingModule) extends Injectable { val userRepository = inject[UserRepository] val maxPoolSize = injectIfBound[Int]('maxThreadPoolSize) { 15 } val myAnalyzer = injectIfMissing[Analyzer](webAnalyzer) ... } -
Configuring your app implicit val bindingModule = ProjectConfiguration val
topInstance = new UserService("myUserService") -
Configuring your app implicit val bindingModule = ProjectConfiguration val
topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration) -
Configuring your app implicit val bindingModule = ProjectConfiguration val
topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration) trait ProjectBindings { implicit val bindingModule: BindingModule = ProjectConfiguration } class MyApp extends Injectable with ProjectBindings { // ... } -
Configuring your app implicit val bindingModule = ProjectConfiguration val
topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration) class SomePage(implicit val bindingModule: BindingModule) extends WicketPage with Injectable { } class ProdSomePage extends SomePage(ProjectConfiguration) trait ProjectBindings { implicit val bindingModule: BindingModule = ProjectConfiguration } class MyApp extends Injectable with ProjectBindings { // ... } -
Tes+ng test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module
=> // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } } -
Tes+ng val testConfig = ProjectConfiguration.modifyBindings { module => //
new calls to bind inside here module } val service = new UserService(“myOtherService”)(testConfig) test(”SubCut scalatest test 2") { // testcode using service goes here } test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module => // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } } -
Tes+ng val testModule = TestProjectConfiguration :: ProjectConfiguration val mergedService
= new UserService("myMergedService")(testModule) test(”SubCut scalatest test 3") { // testcode using mergedService goes here } val testConfig = ProjectConfiguration.modifyBindings { module => // new calls to bind inside here module } val service = new UserService(“myOtherService”)(testConfig) test(”SubCut scalatest test 2") { // testcode using service goes here } test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module => // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } } -
Credits • “Dependency Injec+on Op+ons in Scala” – Dick
Wall @ JavaZone X 2011 – h?p://vimeo.com/28792660 • h?ps://github.com/dickwall/subcut • h?ps://github.com/dickwall/subcut/blob/ master/Ge`ngStarted.md • h?p://stackoverflow.com/ques+ons/7029268/ use-‐subcut-‐scala-‐di-‐on-‐android • h?ps://github.com/janwe/javaBin-‐ScalaDI -
BEKK CONSULTING AS SKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM,
0102 OSLO, NORWAY. WWW.BEKK.NO Jan Fredrik Wedén Manager, Trondheim @jfweden