Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TypeScriptでDIしてみた
Search
Ryohei Hisamitsu
January 16, 2021
Technology
0
520
TypeScriptでDIしてみた
Ryohei Hisamitsu
January 16, 2021
Tweet
Share
More Decks by Ryohei Hisamitsu
See All by Ryohei Hisamitsu
re:Invent参加フィードバック会
hisami
0
91
GoのアプリをEKS(Fargate)にデプロイしてみた話
hisami
0
250
Greengrassを使ったIoTのアーキテクチャ
hisami
0
160
ECS Execを使ってFargateにログインしてみた
hisami
1
380
Other Decks in Technology
See All in Technology
歴史から学ぶ、Goのメモリ管理基礎
logica0419
2
120
Authlete で実装する MCP OAuth 認可サーバー #CIMD の実装を添えて
watahani
0
320
AI時代のアジャイルチームを目指して ー スクラムというコンフォートゾーンからの脱却 ー / Toward Agile Teams in the Age of AI
takaking22
2
570
戰略轉變:從建構 AI 代理人到發展可擴展的技能生態系統
appleboy
0
170
業務の煩悩を祓うAI活用術108選 / AI 108 Usages
smartbank
9
19k
Bedrock AgentCore Evaluationsで学ぶLLM as a judge入門
shichijoyuhi
2
310
ESXi のAIOps だ!2025冬
unnowataru
0
460
コールドスタンバイ構成でCDは可能か
hiramax
0
130
First-Principles-of-Scrum
hiranabe
1
230
「駆動」って言葉、なんかカッコイイ_Mitz
comucal
PRO
0
130
M&Aで拡大し続けるGENDAのデータ活用を促すためのDatabricks権限管理 / AEON TECH HUB #22
genda
0
310
国井さんにPurview の話を聞く会
sophiakunii
1
190
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
463
34k
The Spectacular Lies of Maps
axbom
PRO
1
410
エンジニアに許された特別な時間の終わり
watany
106
220k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.7k
How to Talk to Developers About Accessibility
jct
1
93
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
3
2k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.5k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
39
A designer walks into a library…
pauljervisheath
210
24k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
42
Transcript
で してみた NCDC株式会社 久光遼平
DI(Dependency Injection)とは? • 依存性(Dependency)の注入(Injection) • もう少しわかりやすく あるクラスが依存している要素をクラスの外側から注入すること ClassA { private
classB constructor( ClassBInterface classB ) { this.classB = classB; } } ClassA { private classB constructor() { this.classB = new ClassB(); } }
何が嬉しいの? • 単体テストがしやすくなる! class Service { private repository; constructor() {
this.repository = new Repository(); } find() { // ... this.repository.find(); return; } } よくあるテストしにくい例 テストしたいのはService層のロジック Repository(DB)のテストも含まれてしまう ・テスト失敗した時の切り分けが面倒 ・実行時間がかかる DI→テスト側からモックを注入できる!
実際のアプリではどうやって注入するの? • DIコンテナという便利な仕組みがある ◦ インスタンスの生成、注入を一括で行ってくれる
TypeScriptのDIライブラリ
②注入する側 ①注入される側 ③DIコンテナ 実装例 ④express
まとめ • DIすればテストしやすい作りにできる • DIの仕組みを作るにはInversifyJSを使えば楽 • あとはテストの時にモックを注入するだけ! • 追記:Jestのモック機能を使えば、DIしなくてもテストは書けそう ◦
DIのメリット→より簡単に自由にテスト書ける