-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
570 lines (445 loc) · 17.3 KB
/
Copy pathindex.html
File metadata and controls
570 lines (445 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Journey to Play 2.4</title>
<meta name="description" content="Scaldi - Playframework 2.4.0 integration presentation">
<meta name="author" content="Oleg Ilyenko">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css" id="theme">
<link rel="stylesheet" href="assets/custom.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<script src="assets/offline/fonts.js"></script>
<script src="//use.typekit.net/kov3tvl.js"></script>
<script>try{Typekit.load();}catch(e){}</script>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link rel="stylesheet" href="assets/highlight/solarized_light.css">
</head>
<body>
<div id="credit" style="display: none"></div>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-background="#febd1d">
<h1>Journey to Play 2.4</h1>
<p>
by Oleg Ilyenko / <a href="https://twitter.com/easyangel">@easyangel</a>
</p>
</section>
<section class="image-section even-more-opaque"
data-background="assets/photos/intro.jpg"
data-credit="by Derek Gavey"
data-image-link="https://www.flickr.com/photos/derekgavey/5528275910">
<h2>Scaldi</h2>
<h3>Lightweight Scala Dependency Injection Library</h3>
</section>
<section>
<h2>Inject Bindings</h2>
<pre><code data-trim contenteditable class="scala">
inject [SomeClass]
inject [Database] (identified by 'remote and by default new Riak)
</code></pre>
</section>
<section>
<h2>Bind</h2>
<pre><code data-trim contenteditable class="scala">
class UserModule extends Module {
bind [MessageService] to new OfficialMessageService
binding identifiedBy "greeting.official" to "Welcome"
}
</code></pre>
<p class="fragment roll-in" data-fragment-index="2">
Usage
</p>
<pre class="fragment roll-in" data-fragment-index="2"><code data-trim contenteditable class="scala">
class OfficialMessageService(implicit inj: Injector)
extends MessageService with Injectable {
val officialGreeting =
inject [String] (identified by "greeting.official")
def getGreetMessage(name: String) =
s"$officialGreeting, $name!"
}
</code></pre>
</section>
<section>
<h2>Injector Composition</h2>
<pre><code data-trim contenteditable class="scala">
def tokenModule = new Module {
bind [Tokens] to new TokenRepo(db = inject [Database])
}
def dbModule = new Module {
bind [Database] to new Riak
}
def appModule = tokenModule :: dbModule
</code></pre>
<p class="fragment fade-in" data-fragment-index="1">
and then test it
</p>
<pre class="fragment roll-in" data-fragment-index="1"><code data-trim contenteditable class="scala">
def mocksModule = new Module {
bind [Database] to new InMemoryDb
}
implicit val testModule = mocksModule :: appModule
</code></pre>
</section>
<section>
<h2>Some Features</h2>
<p class="fragment roll-in" data-fragment-index="1">
Constructor injection
</p>
<pre class="fragment roll-in" data-fragment-index="1"><code data-trim contenteditable class="scala">
class TokenRepo(db: Database, metrics: Metrics) extends Tokens
bind [Tokens] to injected [TokenRepo]
</code></pre>
<p class="fragment roll-in" data-fragment-index="2">
Conditions
</p>
<pre class="fragment roll-in" data-fragment-index="2"><code data-trim contenteditable class="scala">
class UserModule extends Module {
bind [MessageService] when (inDevMode or inTestMode) to
new SimpleMessageService
bind [MessageService] when inProdMode to
new OfficialMessageService
}
</code></pre>
</section>
<section class="image-section even-more-opaque"
data-background="assets/photos/past.jpg"
data-credit="by Neil Moralee"
data-image-link="https://www.flickr.com/photos/neilmoralee/13621977953">
<h2>Past</h2>
</section>
<section>
<h2>"DI" in Play 2.3</h2>
<pre><code data-trim contenteditable class="scala">
object Pets extends Controller {
def show(id : Long) = Action {
Ok(views.html.pet(Pet.findById(id)))
}
}
object Pet {
def findById(id : Long) : Pet =
DB.withConnection({ implicit c =>
SQL(basicQuery + "id = {id}").on("id" -> id)().map(rowToPet)
}).head
}
</code></pre>
</section>
<section>
<h2>DI Library Integration</h2>
<pre><code data-trim contenteditable class="scala">
package play.api
trait GlobalSettings {
def getControllerInstance[A](controllerClass: Class[A]): A = ...
// ...
}
</code></pre>
</section>
<section>
<h2>Play 2.3 Scaldi Integration</h2>
<pre><code class="scala" style="overflow: hidden;">
package scaldi.play
trait ScaldiSupport extends GlobalSettings with Injectable {
def applicationModule: Injector
private var currentInjector: Option[Injector] = None
abstract override def onStart(app: Application) =
currentInjector = Some(createApplicationInjector(app))
abstract override def onStop(app: Application) =
currentInjector foreach (_.destroy())
override def getControllerInstance[A](controllerClass: Class[A]): A =
currentInjector map (_.getBinding(...)) getOrElse (...)
}
</code></pre>
</section>
<section>
<h2>Scaldi Usage</h2>
<pre style="margin: 0"><code class="scala">
class Application(implicit inj: Injector)
extends Controller with Injectable {
val messageService = inject [MessageService]
}
</code></pre>
<pre style="margin: 0" class="fragment roll-in" data-fragment-index="1"><code class="scala">
class MyModule extends Module {
binding to new Application
bind [MessageService] to new OfficialMessageService
}
</code></pre>
<pre style="margin: 0;background-color: #ffeded" class="fragment roll-in" data-fragment-index="2"><code class="scala">
object Global extends GlobalSettings with ScaldiSupport {
def applicationModule = new MyModule :: new SomeOtherModule
}
</code></pre>
<pre style="margin: 0" class="fragment roll-in" data-fragment-index="3"><code class="scala">
GET / @controllers.Application.index
</code></pre>
</section>
<section class="image-section more-opaque"
data-background="assets/photos/play2.4.jpg"
data-credit="by NASA HQ PHOTO"
data-image-link="https://www.flickr.com/photos/nasahqphoto/15603732516">
<h2>Play 2.4</h2>
</section>
<section>
<h2>JSR 330</h2>
<pre><code class="scala" style="overflow: hidden;">
import javax.inject.{Inject, Provider, Singleton}
@Singleton
class RoutesProvider @Inject() (
injector: Injector,
environment: Environment,
configuration: Configuration,
httpConfig: HttpConfiguration) extends Provider[Router] {
// ...
}
@Singleton
class Crypto @Inject() (config: CryptoConfig) {
// ...
}
</code></pre>
</section>
<section>
<h2>Scaldi JSR 330 Support</h2>
<pre><code class="scala" style="overflow: hidden;">
implicit val inj = new Module {
binding to annotated [RoutesProvider]
binding identifiedBy qualifier[SomeQualifier] to annotated [MyService]
binding identifiedBy annotation(SomeQualifierImpl.of("dep1")) to
annotated [MyService]
}
</code></pre>
<p class="fragment roll-in" data-fragment-index="1">
+ <strong>OnDemandAnnotationInjector</strong>
</p>
</section>
<section>
<h2>Under the Hood</h2>
<img src="assets/img/pic.svg" />
</section>
<section>
<h2>BuiltinModule</h2>
<pre><code class="scala" style="overflow: hidden;">
package play.api.inject
class BuiltinModule extends play.api.inject.Module {
def bindings(env: Environment, configuration: Configuration) =
Seq(
bind[ApplicationLifecycle].to(bind[DefaultApplicationLifecycle])
bind[Router].toProvider[RoutesProvider],
bind[ActorSystem].toProvider[ActorSystemProvider],
bind[ExecutionContext].toProvider[ExecutionContextProvider],
// ...
)
}
</code></pre>
</section>
<section>
<h2>Plugins</h2>
<pre><code class="scala" style="overflow: hidden;">
class EhCacheModule extends play.api.inject.Module {
def bindings(environment: Environment, configuration: Configuration) = {
// ...
Seq(
bind[CacheManager].toProvider[CacheManagerProvider],
bind[CacheApi].to(bind[CacheApi]
.qualifiedWith(named(defaultCacheName))),
bind[JavaCacheApi].to[DefaultJavaCacheApi]
)
}
}
</code></pre>
</section>
<section>
<h2>reference.conf</h2>
<pre><code class="scala" style="overflow: hidden;">
play {
modules {
enabled += "play.api.cache.EhCacheModule"
}
cache {
# ...
}
}
</code></pre>
</section>
<section>
<h2>Deprecations</h2>
<ul>
<li><strong>GlobalSettings</strong> should not be used anymore</li>
<li class="fragment roll-in" data-fragment-index="1">Plugin is just another <strong>Module</strong></li>
<li class="fragment roll-in" data-fragment-index="2"><strong>FakeApplication</strong> is just a helper to construct a <strong>Module</strong></li>
</ul>
</section>
<section>
<h2>Using Scaldi in Play 2.4</h2>
<p>
<strong>ScaldiSupport</strong> trait is also deprecated...
</p>
<p class="fragment roll-in" data-fragment-index="1">
... use <strong>application.conf</strong> instead
</p>
<pre class="fragment roll-in" data-fragment-index="1"><code class="scala" style="overflow: hidden;">
play.application.loader = scaldi.play.ScaldiApplicationLoader
play.modules.enabled += "modules.MyModule"
play.modules.enabled += "modules.SomeOtherModule"
play.modules.enabled += "scaldi.play.ControllerInjector"
</code></pre>
</section>
<section>
<h2>ScaldiApplicationLoader</h2>
<pre><code class="scala" style="overflow: hidden;">
package scaldi.play
class ScaldiApplicationLoader extends ApplicationLoader {
def load(context: Context) =
new ScaldiApplicationBuilder()
.in(context.environment)
.loadConfig(context.initialConfiguration)
.prependModule(new Module {
bind [OptionalSourceMapper] to
new OptionalSourceMapper(context.sourceMapper)
bind [WebCommands] to context.webCommands
})
.build
}
</code></pre>
</section>
<section>
<h2>Testing</h2>
<pre><code class="scala" style="overflow: hidden;">
val application = new ScaldiApplicationBuilder()
.prependModule(new TestModule).build()
running(application) {
val home = route(FakeRequest(GET, "/")).get
// ...
}
</code></pre>
<p class="fragment roll-in" data-fragment-index="1">or simply</p>
<pre class="fragment roll-in" data-fragment-index="1"><code class="scala" style="overflow: hidden;">
withScaldiApp(modules = Seq(new TestModule)) {
val home = route(FakeRequest(GET, "/")).get
// ...
}
</code></pre>
</section>
<section>
<h2>Full Control</h2>
<pre><code class="scala" style="overflow: hidden;">
val module = new TestModule :: new MyModule :: new ControllerInjector
withScaldiApp(modules = Seq(module, new EhCacheModule, new BuiltinModule),
loadModules = (_, _) => Seq.empty) {
val home = route(FakeRequest(GET, "/")).get
// ...
}
</code></pre>
</section>
<section>
<h2>build.sbt</h2>
<pre><code class="scala" style="overflow: hidden;">
libraryDependencies +=
"org.scaldi" %% "scaldi-play" % "0.5-play-2.4.0-RC3-10"
</code></pre>
<p class="fragment roll-in" data-fragment-index="1">
Example Project
</p>
<p class="fragment roll-in" data-fragment-index="1">
<a href="https://github.com/OlegIlyenko/scaldi-play-example" target="_blank">https://github.com/OlegIlyenko/scaldi-play-example</a>
</p>
</section>
<section>
<h2>Conclusion</h2>
<ul>
<li>Was <strong>fun</strong> to implement</li>
<li class="fragment roll-in" data-fragment-index="1">
<span class="advantage">👍👍</span> Play finally got proper DI mechanism
<ul>
<li class="fragment roll-in" data-fragment-index="2">
<span class="advantage">👍👍👍</span> No global variables and singletons in future
</li>
</ul>
</li>
<li class="fragment roll-in" data-fragment-index="3">
Despite my dislike of annotations, they are necessary in this case
</li>
<li class="fragment roll-in" data-fragment-index="4">
Took me a weekend<span style="color: darkred">*</span> to implement JSR 330 spec
</li>
<li class="fragment roll-in" data-fragment-index="5">
Lesson learned: TCK does not cover all cases
</li>
<li class="fragment roll-in" data-fragment-index="6">
<span class="advantage">👍</span> Play core and plugins are DI library agnostic
</li>
<li class="fragment roll-in" data-fragment-index="7">
<span class="advantage">👍</span> Compile-time and runtime DI mechanism supported
</li>
</ul>
</section>
<section class="image-section more-opaque"
data-background="assets/photos/future.jpg"
data-credit="by Mike"
data-image-link="https://www.flickr.com/photos/pmiaki/6978183235">
<h2>Future</h2>
</section>
<section>
<ul>
<li>Polish integration code</li>
<li class="fragment roll-in" data-fragment-index="1">Review testing support</li>
<li class="fragment roll-in" data-fragment-index="2">Play 2.3 and 2.4 would be both supported</li>
</ul>
</section>
<section data-background="#febd1d">
<h1>Thank you!</h1>
<p>
<ul class="fragment roll-in" data-fragment-index="2">
<li><a href="http://bit.ly/journey-to-play24" target="_blank">http://bit.ly/journey-to-play24</a></li>
</ul>
</p>
<p>
<ul class="fragment roll-in" data-fragment-index="3">
<li><strong>Scaldi:</strong> <a href="http://scaldi.org" target="_blank">http://scaldi.org/</a></li>
<li><strong>Twitter:</strong> <a href="https://twitter.com/easyangel" target="_blank">https://twitter.com/easyangel</a></li>
</ul>
</p>
<h2 class="fragment roll-in" data-fragment-index="4">Questions?</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script src="assets/jquery-2.1.3.min.js"></script>
<script src="assets/custom.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>