SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
The no-framework Scala
Dependency Injection
framework
11/12/2013 BuildStuff 2013

Adam Warski

@adamwarski
BuildStuff 2013
You don’t need anything special to
do Dependency Injection

11/12/2013 BuildStuff 2013

@adamwarski
We often over-complicate

11/12/2013 BuildStuff 2013

@adamwarski
Who Am I?
❖

Day: coding @ SoftwareMill!

❖

Afternoon: playgrounds, Duplos, etc.!

❖

Evenings: blogging, open-source!
❖

Original author of Hibernate Envers!

❖

ElasticMQ, Veripacks, MacWire!

❖

http://www.warski.org

11/12/2013 BuildStuff 2013

@adamwarski
What is Dependency
Injection?

11/12/2013 BuildStuff 2013

@adamwarski
What is DI?
class PresentPackager {!
! def wrap() {!
! ! new RibbonSelector().selectRandom()!
! ! …!
} !
}
11/12/2013 BuildStuff 2013

@adamwarski
What is DI?
class PresentPackager(rs: RibbonSelector) {!
! def wrap() {!
! ! rs.selectRandom()!
! ! …!
} !
}
11/12/2013 BuildStuff 2013

@adamwarski
Yes, DI is just using parameters

11/12/2013 BuildStuff 2013

@adamwarski
Why?
❖

Restrict the knowledge of the class

class PresentPackager {




class PresentPackager

! (rs: RibbonSelector) {


!
!
!
!
!
}

def wrap() {

! new RibbonSelector()

! ! .selectRandom()

! …

}


11/12/2013 BuildStuff 2013



!
!
!
!
}

def wrap() {

! rs.selectRandom()

! …

}


@adamwarski
But still …

❖

We need to have the news somewhere

11/12/2013 BuildStuff 2013

@adamwarski
Let’s create a DI container!
a.k.a. framework

11/12/2013 BuildStuff 2013

@adamwarski
DI in Java
❖

Many frameworks!

❖

Configuration via:!
❖

XML!

❖

annotations!

❖

Java

11/12/2013 BuildStuff 2013

@adamwarski
What’s wrong with that?

❖

Do I really need a DI framework?

11/12/2013 BuildStuff 2013

@adamwarski
Let’s go back …

❖

… and just use our host language!

❖

in this case, Scala!

❖

mapping DI framework concepts to native language
constructs

11/12/2013 BuildStuff 2013

@adamwarski
Manual DI!
!

object PresentWrapper extends App {!
!
!

val ribbonSelector = 

! ! ! new RibbonSelector()!

!
!

val wrappingPaperFeeder = 

! ! ! new WrappingPaperFeeder()!

!
!
!
!

val presentPackager = 

! ! ! new PresentPackager(

! ! ! ! ribbonSelector, 

! ! ! ! wrappingPaperFeeder)!

}
11/12/2013 BuildStuff 2013

@adamwarski
Manual DI!
!

object PresentWrapper extends App {!
!
!

lazy val ribbonSelector = 

! ! ! new RibbonSelector()!

!
!

lazy val wrappingPaperFeeder = 

! ! ! new WrappingPaperFeeder()!

!
!
!
!

lazy
! !
! !
! !

val presentPackager = 

! new PresentPackager(

! ! ribbonSelector, 

! ! wrappingPaperFeeder)!

}
11/12/2013 BuildStuff 2013

@adamwarski
MacWire
import com.softwaremill.macwire.MacwireMacros._!
object PresentWrapper extends App {!
!
!

lazy val ribbonSelector = 

! ! ! wire[RibbonSelector]!

!
!

lazy val wrappingPaperFeeder =

! ! ! wire[WrappingPaperFeeder]!

!
!

lazy val presentPackager =

! ! ! wire[PresentPackager]




}
11/12/2013 BuildStuff 2013

@adamwarski
Side-note: 

Scala Macros

11/12/2013 BuildStuff 2013

@adamwarski
Side-note: Scala Macros

❖

Scala code executed at compile time!

❖

Operate on trees!

❖

Can inspect the environment, generate code!
❖

the code is type-checked

11/12/2013 BuildStuff 2013

@adamwarski
Side-note: Scala Macros
❖

E.g. debug macro!

def debug(params: Any*) = macro debug_impl!
def debug_impl

! ! ! (c: Context)

! ! ! (params: c.Expr[Any]*): c.Expr[Unit]!
!

debug(presentCount) ⟹!
! println(“presentCount = “ + presentCount)
11/12/2013 BuildStuff 2013

@adamwarski
Side-note: Scala Macros
❖

Debug macro implementation!

import c.universe._!
val paramRep = show(param.tree)

val paramRepTree = Literal(Constant(paramRep))

val paramRepExpr = c.Expr[String](paramRepTree)!
reify { println(

! paramRepExpr.splice + 

! " = " + 

! param.splice) }
11/12/2013 BuildStuff 2013

@adamwarski
Side-note: Scala Macros
❖

MacWire!

def wire[T] = macro wire_impl[T]!
def wire_impl

! ! ! [T: c.WeakTypeTag]

! ! ! (c: Context): c.Expr[T]

11/12/2013 BuildStuff 2013

@adamwarski
MacWire
import com.softwaremill.macwire.MacwireMacros._!
object PresentWrapper extends App {!
! lazy val ribbonSelector = 

! ! ! ! wire[RibbonSelector]!
! lazy val wrappingPaperFeeder =

! ! ! ! wire[WrappingPaperFeeder]!
! lazy val presentPackager =

! ! ! ! wire[PresentPackager]

}
11/12/2013 BuildStuff 2013

@adamwarski
Scopes

❖

How long will an object (instance) live?

11/12/2013 BuildStuff 2013

@adamwarski
Singleton & dependent
object NorthPole extends App {!
! // Singleton!
lazy val santaClaus = wire[SantaClaus]!
!

! // Dependent!
def gnome = wire[Gnome]!
}
11/12/2013 BuildStuff 2013

@adamwarski
Arbitrary scopes
trait WebFrontEnd {

! lazy val loggedInUser = 

! ! session(new LoggedInUser)



! def session: Scope

}!
!

trait Scope {

! def apply(factory: => T): T

}
11/12/2013 BuildStuff 2013

@adamwarski
Arbitrary scopes
object MyApp extends WebFrontEnd {

! val session: Scope = 

! ! ! ! new ThreadLocalScope()!
! val filter = new ScopeFilter(session)



! // bootstrap the web server 

! // using the filter

}
11/12/2013 BuildStuff 2013

@adamwarski
Arbitrary scopes
class ScopeFilter(sessionScope: ThreadLocalScope) 

! ! ! extends Filter {!
! def doFilter(request: ServletRequest) {

! ! sessionScope

! ! ! .withStorage(request.getSession()) {!
! ! ! request.proceed()!
! ! }

! }

}
11/12/2013 BuildStuff 2013

@adamwarski
Modules
❖

Pre-wired!

❖

Composable!

❖

Dependencies!

❖

Module per package?!
❖

Veripacks :)

11/12/2013 BuildStuff 2013

@adamwarski
Modules

❖

Module: trait!

❖

Pre-wired: new, MacWire!

❖

Composable: extends/with!

❖

Dependencies: extends/with / abstract members

11/12/2013 BuildStuff 2013

@adamwarski
Modules
trait PresentWrapper {!
! lazy val ribbonSelector = 

! ! ! ! wire[RibbonSelector]!
! lazy val wrappingPaperFeeder =

! ! ! ! wire[WrappingPaperFeeder]!
lazy val presentPackager =

! ! ! ! wire[PresentPackager]!
}
11/12/2013 BuildStuff 2013

@adamwarski
Modules
trait PresentFactory extends PresentWrapper {!
! lazy val teddyBearProvider = 

! ! ! ! wire[TeddyBearProvider]!
! lazy val toyTrainProvider =

! ! ! ! wire[ToyTrainProvider]!
lazy val presentAssembly =

! ! ! ! wire[PresentAssembly]!
}
11/12/2013 BuildStuff 2013

@adamwarski
Modules
trait HomeOfSanta {!
! lazy val santaClaus = wire[SantaClaus]!
! lazy val rudolf = wire[Rudolf]!
! lazy val fireplace = wire[Fireplace]!
!

def presentAssembly: PresentAssembly!
}
11/12/2013 BuildStuff 2013

@adamwarski
Modules
trait PresentWrapper { … }

trait PresentFactory extends PresentWrapper { }

trait HomeOfSanta { … }!
!

object NorthPole 

! extends PresentWrapper 

!
with PresentFactory 

!
with HomeOfSanta {!
! santaClaus.deliver()!
}
11/12/2013 BuildStuff 2013

@adamwarski
Testing Santa’s Home
class HomeOfSantaTest extends FlatSpec {!
! it should “deliver presents” in {!
! ! val mockPresentAssembly = …!
! ! new HomeOfSanta {!
!! lazy val presentAssembly = 

! ! ! ! mockPresentAssembly }!
! ! …!
! }!
}
11/12/2013 BuildStuff 2013

@adamwarski
Cake Pattern
trait PresentPackagerModule {!
!
!
!
!
!
!

class PresentPackager {

! def wrap() {

! ! ribbonSelector.selectRandom()

! ! …

! }

}


! lazy val presentPackager = new PresentPackager()

! def ribbonSelector: RibbonSelector

}
11/12/2013 BuildStuff 2013

@adamwarski
Cake Pattern
val cake = new PresentPackagerModule

! with RibbonSelectorModule

! with WrappingPaperFeederModule

! with TeddyBearProviderModule

! with ToyTrainProviderModule

! with PresentAssemblyModule

! with … { }

11/12/2013 BuildStuff 2013

@adamwarski
Other features
❖

Interceptors!
!

trait Chimney {!
! lazy val presentTransferer =

! ! transactional(wire[PresentTransferer])!
! def transactional: Interceptor!
}
11/12/2013 BuildStuff 2013

@adamwarski
Other features
❖

Factories!
❖

a dedicated object or …!

!

trait PresentBoxer {!
! def box(size: Size) = wire[Box]!
}
11/12/2013 BuildStuff 2013

@adamwarski
Other features
❖

Instance maps!
❖

for integrating e.g. with Play!

❖

Factories!

❖

In-method wiring!

❖

More coming, someday :)

11/12/2013 BuildStuff 2013

@adamwarski
Summing up
❖

Reconsider using a framework!

❖

Native Scala gives a lot of power!
❖
❖

❖

use it!
wisely!

More flexibility (less constraints)

11/12/2013 BuildStuff 2013

@adamwarski
Links

❖

http://www.warski.org!

❖

https://github.com/adamw/macwire!

❖

http://springsource.com/

11/12/2013 BuildStuff 2013

@adamwarski
Thanks!

❖

Questions?!

❖

Stickers ->!

❖

adam@warski.org

11/12/2013 BuildStuff 2013

@adamwarski

Contenu connexe

Tendances

The Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQueryThe Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQuerycolinbdclark
 
component: ruby gems for the browser
component: ruby gems for the browsercomponent: ruby gems for the browser
component: ruby gems for the browserTimothy Oxley
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildwebLeo Zhou
 
Web development basics (Part-2)
Web development basics (Part-2)Web development basics (Part-2)
Web development basics (Part-2)Rajat Pratap Singh
 
Best Practices - Mobile Developer Summit
Best Practices - Mobile Developer SummitBest Practices - Mobile Developer Summit
Best Practices - Mobile Developer Summitwolframkriesing
 
Rails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsRails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsDonSchado
 
How does the Internet Work?
How does the Internet Work?How does the Internet Work?
How does the Internet Work?Dina Goldshtein
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
Rapid prototyping with solr - By Erik Hatcher
Rapid prototyping with solr -  By Erik Hatcher Rapid prototyping with solr -  By Erik Hatcher
Rapid prototyping with solr - By Erik Hatcher lucenerevolution
 
JavaScript!
JavaScript!JavaScript!
JavaScript!RTigger
 
Re-Introduction to Third-party Scripting
Re-Introduction to Third-party ScriptingRe-Introduction to Third-party Scripting
Re-Introduction to Third-party Scriptingbenvinegar
 

Tendances (15)

The Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQueryThe Inclusive Web: hands-on with HTML5 and jQuery
The Inclusive Web: hands-on with HTML5 and jQuery
 
遇見 Ruby on Rails
遇見 Ruby on Rails遇見 Ruby on Rails
遇見 Ruby on Rails
 
Web Ninja
Web NinjaWeb Ninja
Web Ninja
 
component: ruby gems for the browser
component: ruby gems for the browsercomponent: ruby gems for the browser
component: ruby gems for the browser
 
Rails traps
Rails trapsRails traps
Rails traps
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
 
Web development basics (Part-2)
Web development basics (Part-2)Web development basics (Part-2)
Web development basics (Part-2)
 
Best Practices - Mobile Developer Summit
Best Practices - Mobile Developer SummitBest Practices - Mobile Developer Summit
Best Practices - Mobile Developer Summit
 
Rails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsRails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on Rails
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
How does the Internet Work?
How does the Internet Work?How does the Internet Work?
How does the Internet Work?
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
Rapid prototyping with solr - By Erik Hatcher
Rapid prototyping with solr -  By Erik Hatcher Rapid prototyping with solr -  By Erik Hatcher
Rapid prototyping with solr - By Erik Hatcher
 
JavaScript!
JavaScript!JavaScript!
JavaScript!
 
Re-Introduction to Third-party Scripting
Re-Introduction to Third-party ScriptingRe-Introduction to Third-party Scripting
Re-Introduction to Third-party Scripting
 

Similaire à The no-framework Scala Dependency Injection Framework

Using Ember to Make a Bazillion Dollars
Using Ember to Make a Bazillion DollarsUsing Ember to Make a Bazillion Dollars
Using Ember to Make a Bazillion DollarsMike Pack
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaKazuhiro Sera
 
Beginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccBeginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccKazuhiro Sera
 
Lasso and Couchdb : the happy couple
Lasso and Couchdb : the happy coupleLasso and Couchdb : the happy couple
Lasso and Couchdb : the happy coupleAri Najarian
 
Lessons I Learned While Scaling to 5000 Puppet Agents
Lessons I Learned While Scaling to 5000 Puppet AgentsLessons I Learned While Scaling to 5000 Puppet Agents
Lessons I Learned While Scaling to 5000 Puppet AgentsPuppet
 
.NET Development for SQL Server Developer
.NET Development for SQL Server Developer.NET Development for SQL Server Developer
.NET Development for SQL Server DeveloperMarco Parenzan
 
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at Ooyala
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at OoyalaCassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at Ooyala
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at OoyalaDataStax Academy
 
DocuOps & Asciidoctor in a JVM World
DocuOps & Asciidoctor in a JVM WorldDocuOps & Asciidoctor in a JVM World
DocuOps & Asciidoctor in a JVM WorldSchalk Cronjé
 
The Modern Java Web Developer Bootcamp - Devoxx 2013
The Modern Java Web Developer Bootcamp - Devoxx 2013The Modern Java Web Developer Bootcamp - Devoxx 2013
The Modern Java Web Developer Bootcamp - Devoxx 2013Matt Raible
 
Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!Derek Willian Stavis
 
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...Alexander Dean
 
Modern Front-End Development
Modern Front-End DevelopmentModern Front-End Development
Modern Front-End Developmentmwrather
 
MongoTalk/Voyage
MongoTalk/VoyageMongoTalk/Voyage
MongoTalk/VoyageESUG
 
ACCU 2013 Taking Scala into the Enterpise
ACCU 2013 Taking Scala into the EnterpiseACCU 2013 Taking Scala into the Enterpise
ACCU 2013 Taking Scala into the EnterpisePeter Pilgrim
 
From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)Joseph Chiang
 
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, VeronaThe nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, VeronaBoris Hristov
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźAEM HUB
 

Similaire à The no-framework Scala Dependency Injection Framework (20)

Using Ember to Make a Bazillion Dollars
Using Ember to Make a Bazillion DollarsUsing Ember to Make a Bazillion Dollars
Using Ember to Make a Bazillion Dollars
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Beginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccBeginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_ccc
 
Lasso and Couchdb : the happy couple
Lasso and Couchdb : the happy coupleLasso and Couchdb : the happy couple
Lasso and Couchdb : the happy couple
 
Lessons I Learned While Scaling to 5000 Puppet Agents
Lessons I Learned While Scaling to 5000 Puppet AgentsLessons I Learned While Scaling to 5000 Puppet Agents
Lessons I Learned While Scaling to 5000 Puppet Agents
 
.NET Development for SQL Server Developer
.NET Development for SQL Server Developer.NET Development for SQL Server Developer
.NET Development for SQL Server Developer
 
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at Ooyala
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at OoyalaCassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at Ooyala
Cassandra Meetup: Real-time Analytics using Cassandra, Spark and Shark at Ooyala
 
DocuOps & Asciidoctor in a JVM World
DocuOps & Asciidoctor in a JVM WorldDocuOps & Asciidoctor in a JVM World
DocuOps & Asciidoctor in a JVM World
 
The Modern Java Web Developer Bootcamp - Devoxx 2013
The Modern Java Web Developer Bootcamp - Devoxx 2013The Modern Java Web Developer Bootcamp - Devoxx 2013
The Modern Java Web Developer Bootcamp - Devoxx 2013
 
Sprockets
SprocketsSprockets
Sprockets
 
Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!
 
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...
From Zero to Hadoop: a tutorial for getting started writing Hadoop jobs on Am...
 
Modern Front-End Development
Modern Front-End DevelopmentModern Front-End Development
Modern Front-End Development
 
MongoTalk/Voyage
MongoTalk/VoyageMongoTalk/Voyage
MongoTalk/Voyage
 
ACCU 2013 Taking Scala into the Enterpise
ACCU 2013 Taking Scala into the EnterpiseACCU 2013 Taking Scala into the Enterpise
ACCU 2013 Taking Scala into the Enterpise
 
From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)
 
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, VeronaThe nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz Niedźwiedź
 
Refresh OKC
Refresh OKCRefresh OKC
Refresh OKC
 

Plus de Adam Warski

What have the annotations done to us?
What have the annotations done to us?What have the annotations done to us?
What have the annotations done to us?Adam Warski
 
Hejdyk maleńka część mazur
Hejdyk maleńka część mazurHejdyk maleńka część mazur
Hejdyk maleńka część mazurAdam Warski
 
Slick eventsourcing
Slick eventsourcingSlick eventsourcing
Slick eventsourcingAdam Warski
 
Evaluating persistent, replicated message queues
Evaluating persistent, replicated message queuesEvaluating persistent, replicated message queues
Evaluating persistent, replicated message queuesAdam Warski
 
ElasticMQ: a fully asynchronous, Akka-based SQS server
ElasticMQ: a fully asynchronous, Akka-based SQS serverElasticMQ: a fully asynchronous, Akka-based SQS server
ElasticMQ: a fully asynchronous, Akka-based SQS serverAdam Warski
 
Recommendation systems with Mahout: introduction
Recommendation systems with Mahout: introductionRecommendation systems with Mahout: introduction
Recommendation systems with Mahout: introductionAdam Warski
 
CDI Portable Extensions
CDI Portable ExtensionsCDI Portable Extensions
CDI Portable ExtensionsAdam Warski
 

Plus de Adam Warski (8)

What have the annotations done to us?
What have the annotations done to us?What have the annotations done to us?
What have the annotations done to us?
 
Hejdyk maleńka część mazur
Hejdyk maleńka część mazurHejdyk maleńka część mazur
Hejdyk maleńka część mazur
 
Slick eventsourcing
Slick eventsourcingSlick eventsourcing
Slick eventsourcing
 
Evaluating persistent, replicated message queues
Evaluating persistent, replicated message queuesEvaluating persistent, replicated message queues
Evaluating persistent, replicated message queues
 
ElasticMQ: a fully asynchronous, Akka-based SQS server
ElasticMQ: a fully asynchronous, Akka-based SQS serverElasticMQ: a fully asynchronous, Akka-based SQS server
ElasticMQ: a fully asynchronous, Akka-based SQS server
 
Recommendation systems with Mahout: introduction
Recommendation systems with Mahout: introductionRecommendation systems with Mahout: introduction
Recommendation systems with Mahout: introduction
 
Scala Macros
Scala MacrosScala Macros
Scala Macros
 
CDI Portable Extensions
CDI Portable ExtensionsCDI Portable Extensions
CDI Portable Extensions
 

Dernier

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

The no-framework Scala Dependency Injection Framework

  • 1. The no-framework Scala Dependency Injection framework 11/12/2013 BuildStuff 2013 Adam Warski
 @adamwarski BuildStuff 2013
  • 2. You don’t need anything special to do Dependency Injection 11/12/2013 BuildStuff 2013 @adamwarski
  • 3. We often over-complicate 11/12/2013 BuildStuff 2013 @adamwarski
  • 4. Who Am I? ❖ Day: coding @ SoftwareMill! ❖ Afternoon: playgrounds, Duplos, etc.! ❖ Evenings: blogging, open-source! ❖ Original author of Hibernate Envers! ❖ ElasticMQ, Veripacks, MacWire! ❖ http://www.warski.org 11/12/2013 BuildStuff 2013 @adamwarski
  • 5. What is Dependency Injection? 11/12/2013 BuildStuff 2013 @adamwarski
  • 6. What is DI? class PresentPackager {! ! def wrap() {! ! ! new RibbonSelector().selectRandom()! ! ! …! } ! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 7. What is DI? class PresentPackager(rs: RibbonSelector) {! ! def wrap() {! ! ! rs.selectRandom()! ! ! …! } ! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 8. Yes, DI is just using parameters 11/12/2013 BuildStuff 2013 @adamwarski
  • 9. Why? ❖ Restrict the knowledge of the class class PresentPackager {
 
 class PresentPackager
 ! (rs: RibbonSelector) {
 ! ! ! ! ! } def wrap() {
 ! new RibbonSelector()
 ! ! .selectRandom()
 ! …
 }
 11/12/2013 BuildStuff 2013 
 ! ! ! ! } def wrap() {
 ! rs.selectRandom()
 ! …
 }
 @adamwarski
  • 10. But still … ❖ We need to have the news somewhere 11/12/2013 BuildStuff 2013 @adamwarski
  • 11. Let’s create a DI container! a.k.a. framework 11/12/2013 BuildStuff 2013 @adamwarski
  • 12. DI in Java ❖ Many frameworks! ❖ Configuration via:! ❖ XML! ❖ annotations! ❖ Java 11/12/2013 BuildStuff 2013 @adamwarski
  • 13. What’s wrong with that? ❖ Do I really need a DI framework? 11/12/2013 BuildStuff 2013 @adamwarski
  • 14. Let’s go back … ❖ … and just use our host language! ❖ in this case, Scala! ❖ mapping DI framework concepts to native language constructs 11/12/2013 BuildStuff 2013 @adamwarski
  • 15. Manual DI! ! object PresentWrapper extends App {! ! ! val ribbonSelector = 
 ! ! ! new RibbonSelector()! ! ! val wrappingPaperFeeder = 
 ! ! ! new WrappingPaperFeeder()! ! ! ! ! val presentPackager = 
 ! ! ! new PresentPackager(
 ! ! ! ! ribbonSelector, 
 ! ! ! ! wrappingPaperFeeder)! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 16. Manual DI! ! object PresentWrapper extends App {! ! ! lazy val ribbonSelector = 
 ! ! ! new RibbonSelector()! ! ! lazy val wrappingPaperFeeder = 
 ! ! ! new WrappingPaperFeeder()! ! ! ! ! lazy ! ! ! ! ! ! val presentPackager = 
 ! new PresentPackager(
 ! ! ribbonSelector, 
 ! ! wrappingPaperFeeder)! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 17. MacWire import com.softwaremill.macwire.MacwireMacros._! object PresentWrapper extends App {! ! ! lazy val ribbonSelector = 
 ! ! ! wire[RibbonSelector]! ! ! lazy val wrappingPaperFeeder =
 ! ! ! wire[WrappingPaperFeeder]! ! ! lazy val presentPackager =
 ! ! ! wire[PresentPackager]
 
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 18. Side-note: 
 Scala Macros 11/12/2013 BuildStuff 2013 @adamwarski
  • 19. Side-note: Scala Macros ❖ Scala code executed at compile time! ❖ Operate on trees! ❖ Can inspect the environment, generate code! ❖ the code is type-checked 11/12/2013 BuildStuff 2013 @adamwarski
  • 20. Side-note: Scala Macros ❖ E.g. debug macro! def debug(params: Any*) = macro debug_impl! def debug_impl
 ! ! ! (c: Context)
 ! ! ! (params: c.Expr[Any]*): c.Expr[Unit]! ! debug(presentCount) ⟹! ! println(“presentCount = “ + presentCount) 11/12/2013 BuildStuff 2013 @adamwarski
  • 21. Side-note: Scala Macros ❖ Debug macro implementation! import c.universe._! val paramRep = show(param.tree)
 val paramRepTree = Literal(Constant(paramRep))
 val paramRepExpr = c.Expr[String](paramRepTree)! reify { println(
 ! paramRepExpr.splice + 
 ! " = " + 
 ! param.splice) } 11/12/2013 BuildStuff 2013 @adamwarski
  • 22. Side-note: Scala Macros ❖ MacWire! def wire[T] = macro wire_impl[T]! def wire_impl
 ! ! ! [T: c.WeakTypeTag]
 ! ! ! (c: Context): c.Expr[T] 11/12/2013 BuildStuff 2013 @adamwarski
  • 23. MacWire import com.softwaremill.macwire.MacwireMacros._! object PresentWrapper extends App {! ! lazy val ribbonSelector = 
 ! ! ! ! wire[RibbonSelector]! ! lazy val wrappingPaperFeeder =
 ! ! ! ! wire[WrappingPaperFeeder]! ! lazy val presentPackager =
 ! ! ! ! wire[PresentPackager]
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 24. Scopes ❖ How long will an object (instance) live? 11/12/2013 BuildStuff 2013 @adamwarski
  • 25. Singleton & dependent object NorthPole extends App {! ! // Singleton! lazy val santaClaus = wire[SantaClaus]! ! ! // Dependent! def gnome = wire[Gnome]! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 26. Arbitrary scopes trait WebFrontEnd {
 ! lazy val loggedInUser = 
 ! ! session(new LoggedInUser)
 
 ! def session: Scope
 }! ! trait Scope {
 ! def apply(factory: => T): T
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 27. Arbitrary scopes object MyApp extends WebFrontEnd {
 ! val session: Scope = 
 ! ! ! ! new ThreadLocalScope()! ! val filter = new ScopeFilter(session)
 
 ! // bootstrap the web server 
 ! // using the filter
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 28. Arbitrary scopes class ScopeFilter(sessionScope: ThreadLocalScope) 
 ! ! ! extends Filter {! ! def doFilter(request: ServletRequest) {
 ! ! sessionScope
 ! ! ! .withStorage(request.getSession()) {! ! ! ! request.proceed()! ! ! }
 ! }
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 30. Modules ❖ Module: trait! ❖ Pre-wired: new, MacWire! ❖ Composable: extends/with! ❖ Dependencies: extends/with / abstract members 11/12/2013 BuildStuff 2013 @adamwarski
  • 31. Modules trait PresentWrapper {! ! lazy val ribbonSelector = 
 ! ! ! ! wire[RibbonSelector]! ! lazy val wrappingPaperFeeder =
 ! ! ! ! wire[WrappingPaperFeeder]! lazy val presentPackager =
 ! ! ! ! wire[PresentPackager]! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 32. Modules trait PresentFactory extends PresentWrapper {! ! lazy val teddyBearProvider = 
 ! ! ! ! wire[TeddyBearProvider]! ! lazy val toyTrainProvider =
 ! ! ! ! wire[ToyTrainProvider]! lazy val presentAssembly =
 ! ! ! ! wire[PresentAssembly]! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 33. Modules trait HomeOfSanta {! ! lazy val santaClaus = wire[SantaClaus]! ! lazy val rudolf = wire[Rudolf]! ! lazy val fireplace = wire[Fireplace]! ! def presentAssembly: PresentAssembly! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 34. Modules trait PresentWrapper { … }
 trait PresentFactory extends PresentWrapper { }
 trait HomeOfSanta { … }! ! object NorthPole 
 ! extends PresentWrapper 
 ! with PresentFactory 
 ! with HomeOfSanta {! ! santaClaus.deliver()! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 35. Testing Santa’s Home class HomeOfSantaTest extends FlatSpec {! ! it should “deliver presents” in {! ! ! val mockPresentAssembly = …! ! ! new HomeOfSanta {! !! lazy val presentAssembly = 
 ! ! ! ! mockPresentAssembly }! ! ! …! ! }! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 36. Cake Pattern trait PresentPackagerModule {! ! ! ! ! ! ! class PresentPackager {
 ! def wrap() {
 ! ! ribbonSelector.selectRandom()
 ! ! …
 ! }
 }
 ! lazy val presentPackager = new PresentPackager()
 ! def ribbonSelector: RibbonSelector
 } 11/12/2013 BuildStuff 2013 @adamwarski
  • 37. Cake Pattern val cake = new PresentPackagerModule
 ! with RibbonSelectorModule
 ! with WrappingPaperFeederModule
 ! with TeddyBearProviderModule
 ! with ToyTrainProviderModule
 ! with PresentAssemblyModule
 ! with … { } 11/12/2013 BuildStuff 2013 @adamwarski
  • 38. Other features ❖ Interceptors! ! trait Chimney {! ! lazy val presentTransferer =
 ! ! transactional(wire[PresentTransferer])! ! def transactional: Interceptor! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 39. Other features ❖ Factories! ❖ a dedicated object or …! ! trait PresentBoxer {! ! def box(size: Size) = wire[Box]! } 11/12/2013 BuildStuff 2013 @adamwarski
  • 40. Other features ❖ Instance maps! ❖ for integrating e.g. with Play! ❖ Factories! ❖ In-method wiring! ❖ More coming, someday :) 11/12/2013 BuildStuff 2013 @adamwarski
  • 41. Summing up ❖ Reconsider using a framework! ❖ Native Scala gives a lot of power! ❖ ❖ ❖ use it! wisely! More flexibility (less constraints) 11/12/2013 BuildStuff 2013 @adamwarski