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
75
GoのアプリをEKS(Fargate)にデプロイしてみた話
hisami
0
230
Greengrassを使ったIoTのアーキテクチャ
hisami
0
130
ECS Execを使ってFargateにログインしてみた
hisami
1
340
Other Decks in Technology
See All in Technology
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
2
610
登壇ネタの見つけ方 / How to find talk topics
pinkumohikan
5
540
2025-06-26 GitHub CopilotとAI駆動開発:実践と導入のリアル
fl_kawachi
1
130
A2Aのクライアントを自作する
rynsuke
1
220
250627 関西Ruby会議08 前夜祭 RejectKaigi「DJ on Ruby Ver.0.1」
msykd
PRO
2
340
AI導入の理想と現実~コストと浸透〜
oprstchn
0
110
ひとり情シスなCTOがLLMと始めるオペレーション最適化 / CTO's LLM-Powered Ops
yamitzky
0
450
"サービスチーム" での技術選定 / Making Technology Decisions for the Service Team
kaminashi
1
190
Amazon S3標準/ S3 Tables/S3 Express One Zoneを使ったログ分析
shigeruoda
4
570
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
260
SpringBoot x TestContainerで実現するポータブル自動結合テスト
demaecan
0
110
「Chatwork」の認証基盤の移行とログ活用によるプロダクト改善
kubell_hr
1
210
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Typedesign – Prime Four
hannesfritz
42
2.7k
Practical Orchestrator
shlominoach
188
11k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Docker and Python
trallard
44
3.4k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
RailsConf 2023
tenderlove
30
1.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
710
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
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のメリット→より簡単に自由にテスト書ける