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
510
TypeScriptでDIしてみた
Ryohei Hisamitsu
January 16, 2021
Tweet
Share
More Decks by Ryohei Hisamitsu
See All by Ryohei Hisamitsu
re:Invent参加フィードバック会
hisami
0
78
GoのアプリをEKS(Fargate)にデプロイしてみた話
hisami
0
230
Greengrassを使ったIoTのアーキテクチャ
hisami
0
130
ECS Execを使ってFargateにログインしてみた
hisami
1
350
Other Decks in Technology
See All in Technology
サービスを止めるな! DDoS攻撃へのスマートな備えと最前線の事例
coconala_engineer
1
190
クラウド開発の舞台裏とSRE文化の醸成 / SRE NEXT 2025 Lunch Session
kazeburo
1
610
全部AI、全員Cursor、ドキュメント駆動開発 〜DevinやGeminiも添えて〜
rinchsan
10
5.2k
How to Quickly Call American Airlines®️ U.S. Customer Care : Full Guide
flyaahelpguide
0
240
本当にわかりやすいAIエージェント入門
segavvy
5
2.6k
Autify Company Deck
autifyhq
2
44k
〜『世界中の家族のこころのインフラ』を目指して”次の10年”へ〜 SREが導いたグローバルサービスの信頼性向上戦略とその舞台裏 / Towards the Next Decade: Enhancing Global Service Reliability
kohbis
3
1.5k
Four Keysから始める信頼性の改善 - SRE NEXT 2025
ozakikota
0
420
MCP とマネージド PaaS で実現する大規模 AI アプリケーションの高速開発
nahokoxxx
1
410
ClaudeCode_vs_GeminiCLI_Terraformで比較してみた
tkikuchi
1
2.4k
アクセスピークを制するオートスケール再設計: 障害を乗り越えKEDAで実現したリソース管理の最適化
myamashii
1
710
振り返りTransit Gateway ~VPCをいい感じでつなげるために~
masakiokuda
4
210
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Gamification - CAS2011
davidbonilla
81
5.4k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
The Cost Of JavaScript in 2023
addyosmani
51
8.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
A better future with KSS
kneath
238
17k
BBQ
matthewcrist
89
9.7k
Thoughts on Productivity
jonyablonski
69
4.7k
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のメリット→より簡単に自由にテスト書ける