SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
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...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

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