-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguidelines.html
431 lines (409 loc) · 20.8 KB
/
guidelines.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
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
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="microsite">
<meta name="generator" content="Paradox, paradox-material-theme=0.7.0, mkdocs-material=3.0.3">
<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">
<meta name="description" content="microsite">
<link rel="shortcut icon" href="assets/images/favicon.png">
<title>Izumi Framework Best Practices · Izumi Project</title>
<link rel="stylesheet" href="assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="lib/prettify/prettify.css">
<script src="assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="assets/fonts/font-awesome.css">
<link rel="stylesheet" href="assets/fonts/material-icons.css">
<link rel="stylesheet" href="assets/stylesheets/paradox-material-theme.css">
</head>
<body
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="index.html" title="Izumi Project" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Izumi Project
</span>
<span class="md-header-nav__topic">
Izumi Framework Best Practices
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1"></button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/7mind/izumi"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
7mind/izumi
</div>
</a>
</div>
</div>
</div>
</nav>
</header>
<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="index.html" title="Izumi Project" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="index.html" title="Izumi Project">
Izumi Project
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/7mind/izumi"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
7mind/izumi
</div>
</a>
</div>
<ul>
<li><a href="glossary.html" class="page">Izumi Glossary</a></li>
<li><a href="guidelines.html" class="active page">Izumi Framework Best Practices</a></li>
<li><a href="bio/index.html" class="page">BIO Hierarchy</a></li>
<li><a href="logstage/index.html" class="page">LogStage</a></li>
<li><a href="distage/index.html" class="page">DIStage</a>
<ul>
<li><a href="distage/basics.html" class="page">Overview</a></li>
<li><a href="distage/debugging.html" class="page">Debugging</a></li>
<li><a href="distage/advanced-features.html" class="page">Advanced Features</a></li>
<li><a href="distage/distage-framework.html" class="page">distage-framework</a></li>
<li><a href="distage/distage-framework-docker.html" class="page">distage-framework-docker</a></li>
<li><a href="distage/distage-testkit.html" class="page">distage-testkit</a></li>
<li><a href="distage/reference.html" class="page">Syntax Reference</a></li>
</ul></li>
<li><a href="idealingua/index.html" class="page">IdeaLingua</a>
<ul>
<li><a href="idealingua/language-reference.html" class="page">Idealingua Language Reference</a></li>
<li><a href="idealingua/json.html" class="page">JSON Wire Format</a></li>
<li><a href="idealingua/cogen.html" class="page">Code generator reference</a></li>
<li><a href="idealingua/cogen-circe.html" class="page">Circe serialization reference</a></li>
</ul></li>
<li><a href="sbt/index.html" class="page">SBT Notes</a></li>
<li><a href="manifesto/index.html" class="page">Productivity and challenges</a></li>
<li><a href="pper/index.html" class="page">PPER Pattern</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
<li><a href="guidelines.html#izumi-framework-best-practices" class="header">Izumi Framework Best Practices</a>
<ul>
<li><a href="guidelines.html#general-workflow" class="header">General workflow</a></li>
<li><a href="guidelines.html#bio-tf-and-error-encoding" class="header">BIO, TF and error encoding</a></li>
<li><a href="guidelines.html#dual-test-tactic-and-tdd" class="header">Dual Test Tactic and TDD</a></li>
<li><a href="guidelines.html#data-modeling" class="header">Data modeling</a></li>
<li><a href="guidelines.html#integration-points" class="header">Integration points</a></li>
<li><a href="guidelines.html#inheritance" class="header">Inheritance</a></li>
</ul></li>
</ul>
</nav>
</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.2.16
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
<li><a href="guidelines.html#izumi-framework-best-practices" class="header">Izumi Framework Best Practices</a>
<ul>
<li><a href="guidelines.html#general-workflow" class="header">General workflow</a></li>
<li><a href="guidelines.html#bio-tf-and-error-encoding" class="header">BIO, TF and error encoding</a></li>
<li><a href="guidelines.html#dual-test-tactic-and-tdd" class="header">Dual Test Tactic and TDD</a></li>
<li><a href="guidelines.html#data-modeling" class="header">Data modeling</a></li>
<li><a href="guidelines.html#integration-points" class="header">Integration points</a></li>
<li><a href="guidelines.html#inheritance" class="header">Inheritance</a></li>
</ul></li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#izumi-framework-best-practices" name="izumi-framework-best-practices" class="anchor"><span class="anchor-link"></span></a>Izumi Framework Best Practices</h1>
<h2><a href="#general-workflow" name="general-workflow" class="anchor"><span class="anchor-link"></span></a>General workflow</h2>
<p>When we start working on a new component, generally we follow these steps:</p>
<ol>
<li>Define external data model of the component, namely:</li>
<li>Inputs and outputs of the component methods, as <code>case</code> classes and <code>sealed</code> traits</li>
<li>Expected (domain) error hierarchies, as one <code>sealed trait</code> and <code>final case</code> classes inheriting it</li>
<li>We define abstract interface (<code>trait</code>) of the component,</li>
<li>We use an abstract bifunctor <code>F[+_, +_]</code> in order to encode method outputs</li>
<li>Methods which are not expected to fail should have <code>Nothing</code> in the error (left) branch of the bifunctor</li>
<li>It’s okay to initially have <code>Nothing</code> everywhere and add specific error later, once we discover them</li>
<li>Don’t try to encode all the possible errors, especially irrecoverable ones. E.g. it’s pointless to encode <code>OutOfMemoryError</code> as a possible method result. Only the expected domain errors should be encoded.</li>
<li>Create a stub implementation of the component. If the component is an integration point, its first implementation should be a fake</li>
<li>Use <code>BIO</code> typeclasses as <code>F</code> implementation, if the component is intended to be run under ZIO or another bifunctor, use <code>Either</code> otherwise</li>
<li>Try to use the minimal set of <code>BIO</code> typeclasses necessary for your component. It’s fine to start by using <code>IO2</code> but you should remove unnecessary capabilites when possible.</li>
<li>Keep the stub implementation inside of the companion object of the component interface</li>
<li>Add distage bindings for the component and its implementation</li>
<li>Create a stub of an abstract test suite for the interface</li>
<li>Evaluate your tests in terms of <a href="https://blog.7mind.io/constructive-test-taxonomy.html">Constructive Test Taxonomy</a></li>
<li>Stick to the best/cheapest test kinds (in CTT terms) which would work for your purpose</li>
<li>Try to stick to Blackbox-Contractual tests when you can 6 Work on the test and the implementation (the fake or the business code) simultaneously until you have them functional</li>
<li>It’s not very good but acceptable to use impure mutable fields in dummies instead of <code>Ref</code>s.</li>
<li>If the component is an integration point, start working on a real implementation</li>
<li>Add integration checks and their bindings</li>
<li>Add docker definitions and their bindings for managed scene if that’s possible</li>
</ol>
<h3><a href="#example" name="example" class="anchor"><span class="anchor-link"></span></a>Example</h3>
<pre class="prettyprint"><code class="language-scala">final case class User(props: Map[String, String])
final case class IdentifiedUser(id: UUID, user: User)
sealed trait DatabaseError
final case class EntityNotFoundExists(id: UUID) extends DatabaseError
trait DatabaseLayer[F[+_, +_]] {
def store(id: UUID, user: User): F[Nothing, Unit]
def get(id: UUID): F[EntityNotFoundExists, IdentifiedUser]
}
object DatabaseLayer {
final class DatabaseLayerDummyImpl[F[+_, +_] : IO2]() extends DatabaseLayer[F] {
private val content = scala.collection.mutable.HashMap.empty[UUID, IdentifiedUser]
def store(id: UUID, user: User): F[Nothing, Unit] = F.sync(content += IdentifiedUser(id, user))
def get(id: UUID): F[EntityNotFoundExists, IdentifiedUser] = F.fromOption(EntityNotFoundExists(id))(content.get(id))
}
}
final class DatabaseLayerPostgresImpl[F[+_, +_] : IO2]() extends DatabaseLayer[F] {
// ...
}
class DatabaseModule[F[+_, +_] : TagKK] extends PluginDef {
make[DatabaseLayer[F]].tagged(Repo.Dummy).from[DatabaseLayer.DatabaseLayerDummyImpl[F]]
make[DatabaseLayer[F]].tagged(Repo.Prod).from[DatabaseLayerPostgresImpl[F]]
}
class DatabaseLayerTest extends Spec2[zio.IO] {
override def config: TestConfig = super.config.copy(
pluginConfig = PluginConfig.const(new DatabaseModule[zio.IO])
)
"database layer" should {
"store users" in {
(db: DatabaseLayer[zio.IO]) =>
for {
// ...
} yield {
assert(...)
}
}
}
}
</code></pre>
<h2><a href="#bio-tf-and-error-encoding" name="bio-tf-and-error-encoding" class="anchor"><span class="anchor-link"></span></a>BIO, TF and error encoding</h2>
<ol>
<li>Always use for-comprehension</li>
<li>Always use minimal possible set of BIO typeclasses</li>
<li>You can’t do much with irrecoverable errors. Let them be logged and fail the computation</li>
</ol>
<h3><a href="#error-propagation" name="error-propagation" class="anchor"><span class="anchor-link"></span></a>Error propagation</h3>
<p>If one component uses another, it either should propagate its errors or handle them. This makes things somewhat inconvenient, especially on Scala 2 which has no support for union types.</p>
<p>Essentially, on Scala 2 there is no perfect solution for error propagation.</p>
<p>The most comprehensive solution would look like:</p>
<pre class="prettyprint"><code class="language-scala">sealed trait DatabaseError
final case class EntityNotFoundExists(id: UUID) extends DatabaseError
sealed trait BusinessError
sealed trait UserStoreMethodError {
this: BusinessError =>
}
final case class UserInvalid(id: UUID) extends BusinessError with UserStoreMethodError
final case class InheritedDatabaseError(error: DatabaseError) extends BusinessError with UserStoreMethodError
trait BusinessLayer[F[+_, +_]] {
def validateAndStore(id: UUID, user: User): F[UserStoreMethodError, Unit]
}
</code></pre>
<p>Essentially, this approach simulates the missing union types.</p>
<p>It kinda breaks encapsulation, because the errors specific only to one particular component implementation are being propagated into the abstract interface. Also this approach, when followed strictly, is just way too verbose.</p>
<p>It’s possible to modify it by just logging dependency errors and returning generic failure branch:</p>
<pre class="prettyprint"><code class="language-scala">sealed trait BusinessError
sealed trait UserStoreMethodError {
this: BusinessError =>
}
final case class UserInvalid(id: UUID) extends BusinessError with UserStoreMethodError
// when we return this, we would have to log the details first
final case class InheritedDatabaseError() extends BusinessError with UserStoreMethodError
</code></pre>
<p>In most of the cases you wouldn’t want to create separate unions for every method (like <code>UserStoreMethodError</code>) and would just return the base failure (<code>BusinessError</code>) everywhere:</p>
<pre class="prettyprint"><code class="language-scala">sealed trait BusinessError
final case class UserInvalid(id: UUID) extends BusinessError
final case class InheritedDatabaseError() extends BusinessError
trait BusinessLayer[F[+_, +_]] {
def validateAndStore(id: UUID, user: User): F[BusinessError, Unit]
}
</code></pre>
<p>It’s tempting to use exceptions everywhere either explicitly encoded or just hidden in old bad Java manner. Don’t do that. It’s always possible to have errors encoded as values and, generally, it always pays back.</p>
<p>Only use exceptions and try-catch blocks when you need to integrate with legacy third-party code.</p>
<h2><a href="#dual-test-tactic-and-tdd" name="dual-test-tactic-and-tdd" class="anchor"><span class="anchor-link"></span></a>Dual Test Tactic and TDD</h2>
<ol>
<li>When working on a dummy, don’t try to replicate all the specificity of a real endpoint, unless that’s really important. Dummies should be cheap. There is no point in simulating all the possible errors and conditions specific to real integration points.</li>
<li>Probably, you don’t want to simulate database connection errors in a database layer dummy</li>
<li>Though, if you work on a UDP transport layer component, you might want to simulate packet loss in its dummy</li>
<li>Dual test tactic always pays back, always write dual tests for your integration points</li>
<li>With distage it’s not expensive to have dual tests for your business logic. Do that too.</li>
<li>There is no reason to test all the possible combinations of production/dummy dependencies. Usually it’s enough to have an all-production and an all-dummy test configuration.</li>
</ol>
<h2><a href="#data-modeling" name="data-modeling" class="anchor"><span class="anchor-link"></span></a>Data modeling</h2>
<ol>
<li>Denormalize inputs and outputs. E.g. it’s a common useful pattern to split “raw” “unsaved” entities and “stored” entities retrieved from the database</li>
<li>Make your models closed. All the classes should be <code>final</code>, all traits <code>sealed</code>.</li>
<li>Avoid storing untyped data (e.g. <code>JSON</code>) in your models at all costs</li>
</ol>
<h2><a href="#integration-points" name="integration-points" class="anchor"><span class="anchor-link"></span></a>Integration points</h2>
<ol>
<li>Keep your integration points as dumb as possible, just wrap the APIs you integrate with</li>
<li>Generally, all the assertions, computations and decisions should happen in your business layer</li>
</ol>
<h2><a href="#inheritance" name="inheritance" class="anchor"><span class="anchor-link"></span></a>Inheritance</h2>
<ol>
<li>There is very limited use of <code>class</code> inheritance. Usually one <code>class</code> shouldn’t inherit from another. Make classes <code>final</code> by default. If you intend to use class inheritance, mark them as <code>open</code>, but do not leave them unmarked.</li>
<li><code>abstract</code> classes might be useful but, generally, you should avoid having them too</li>
</ol>
</div>
<div>
<a href="https://github.com/7mind/izumi/tree/v1.2.16/doc/microsite/target/mdoc/guidelines.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.2.16
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="glossary.html" title="Izumi Glossary" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
Izumi Glossary
</span>
</div>
</a>
<a href="bio/index.html" title="BIO Hierarchy" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
BIO Hierarchy
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
<div class="md-footer-copyright__highlight">
7mind.io
</div>
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://sbt.github.io/sbt-paradox-material-theme/">Paradox Material Theme</a>
</div>
</div>
</div>
</footer>
</div>
<script src="assets/javascripts/application.583bbe55.js"></script>
<script src="assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"."}})</script>
<script type="text/javascript" src="lib/prettify/prettify.js"></script>
<script type="text/javascript" src="lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>