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

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Dernier (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

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