-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
235 lines (224 loc) · 7.1 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Groovy Data Loading</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/moon.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- 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>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Data Loading DSL</h1>
<h3>Groovy man</h3>
</section>
<section>
<h2>Why?</h2>
<p class="fragment">
Traditional approaches are cumbersome to create and maintain
</p>
<p class="fragment">
Save time describing data to focus on tests
</p>
</section>
<section>
<section>
<h2>Traditional Approaches</h2>
<p>SQL</p>
<pre><code>
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
INSERT INTO foo VALUES('blah', 'blah', 'blah', 'blah');
</code></pre>
</section>
<section>
<h2>Traditional Approaches</h2>
<p>CSV</p>
<pre><code>
'Header 1', 'Header 2', 'Header 3', 'Header 4', 'Header 5', 'Header 6'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
'blah', 'blah', 'blah', 'blah', 'blah', 'blah'
</code></pre>
</section>
</section>
<section>
<h2>How to do better?</h2>
<p class="fragment">Leverage the stinkin JPA repositories</p>
<p class="fragment">Groovy DSL will save lots of typing</p>
</section>
<section>
<h2>Why Leverage JPA repositories</h2>
<p class="fragment">IDs can still be auto generated</p>
<p class="fragment">Management of related entities is much easier</p>
</section>
<section>
<h2>Possible Java Data Loading</h2>
<pre><code>
public class MyAwesomeDataLoader implements ApplicationRunner {
@Autowired
private WidgetRepository widgetRepository;
@Override
public void run(ApplicationArguments args) throws Exception {
widgetRepository.save(makeWidget("First!", "Secret");
widgetRepository.save(makeWidget("Next One", "A follow up");
// I'm tired of typing ...
}
private Widget makeWidget(String name, String importantAttribute) {
Widget widget = new Widget();
widget.setName(name);
widget.setImportantAttribute(importantAttribute);
return widget;
}
}
</code></pre>
</section>
<section>
<h2>Groovy DSL</h2>
<p class="fragment">Domain Specific Language</p>
<p class="fragment">If you've used Gradle, you've used a DSL</p>
</section>
<section>
<section>
<h2>DSL Magic</h2>
<p>This</p>
<pre><code>
plugins {
id 'org.springframework.boot' version '1.5.7.RELEASE'
}
</code></pre>
<p>Really Means This</p>
<pre><code>
plugins({
id('org.springframework.boot').version('1.5.7.RELEASE')
})
</code></pre>
</section>
<section>
<h2>DSL Magic</h2>
<p>And This</p>
<pre><code>
dependencies {
// spring
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
}
</code></pre>
<p>Really Means This</p>
<pre><code>
dependencies({
// spring
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
})
</code></pre>
</section>
</section>
<section>
<section>
<h2>Key DSL Elements</h2>
<p>Method chains</p>
<pre><code>
a b c d e => a(b).c(d).e
</code></pre>
<pre><code>
a b c() d e => a(b).c().d(e)
</code></pre>
</section>
<section>
<h2>Key DSL Elements</h2>
<p>Closures</p>
<p class="fragment">First class anonymous functions</p>
<p class="fragment">Can control the <em>delegate</em>, the context object</p>
</section>
</section>
<section>
<h2>What We're After</h2>
<pre><code>
save widget { name 'First!' importantAttribute 'Secret' },
widget { name 'Next One' importantAttribute 'A follow up' }
</code></pre>
</section>
<section>
<h2>Two DSL Operations</h2>
<p class="fragment">Create an entity based on dynamic method name and closure</p>
<p class="fragment">Save an entity using the appropriate repository</p>
</section>
<section>
<h2>A class that loads and runs scripts</h2>
<pre><code>
public class DataSeedRunner implements ApplicationRunner {
public void run(ApplicationArguments args) throws Exception {
// Locate all Groovy scripts under some base path
// Parse each script with GroovyShell
// Pass any necessary items to custom script class
// Run the script
}
}
</code></pre>
</section>
<section>
<h2>A custom base Script class</h2>
<pre><code>
public abstract class DataSeedScript extends Script {
public Object invokeMethod(String name, Object args) {
// Method to create entities
// Lookup entity type based on name
// Look for a Builder declared within entity type
// Create a builder and pass it as delegate to the first argument which is a closure
// Run the closure
// Build entity with the builder
}
public List<Object> save(Object... entities) {
// For each entity
// Lookup the JPA repository per the entity's type
// Use the repository to persist the entity
}
}
</code></pre>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>