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
Dependency Injection はじめの一歩 / 20191024 C#Tokyo
Search
Taka
October 24, 2019
Programming
0
840
Dependency Injection はじめの一歩 / 20191024 C#Tokyo
2019/10/24
.NET Core 3.0 リリース記念 C# Tokyo 2019/10 発表資料
Taka
October 24, 2019
Tweet
Share
More Decks by Taka
See All by Taka
フロントエンド初心者が Blazorを使ってみた / 20220428 C#Tokyo
takahiro901
0
470
HoloLens 2なしでも MRを楽しむ!
takahiro901
0
1k
BlazorのPWAを使ってみた / Tried using PWA
takahiro901
0
1.4k
C#勉強法: コンソールでアプリを作ろう / 20190910 C# Tokyo
takahiro901
0
780
Xamarin.Formsで自分用の家計簿アプリを作ってみた / 20190831 JXUG
takahiro901
0
940
プログラミング初心者が Xamarin.Formsやってみた / 20180523 JXUG
takahiro901
0
440
Other Decks in Programming
See All in Programming
Back to the Future: Let me tell you about the ACP protocol
terhechte
0
140
育てるアーキテクチャ:戦い抜くPythonマイクロサービスの設計と進化戦略
fujidomoe
1
170
Devvox Belgium - Agentic AI Patterns
kdubois
1
100
そのpreloadは必要?見過ごされたpreloadが技術的負債として爆発した日
mugitti9
2
3.2k
株式会社 Sun terras カンパニーデック
sunterras
0
260
エンジニアとして高みを目指す、 利益を生み出す設計の考え方 / design-for-profit
minodriven
24
12k
CSC305 Lecture 04
javiergs
PRO
0
260
『毎日の移動』を支えるGoバックエンド内製開発
yutautsugi
2
220
(Extension DC 2025) Actor境界を越える技術
teamhimeh
1
250
複雑化したリポジトリをなんとかした話 pipenvからuvによるモノレポ構成への移行
satoshi256kbyte
1
990
ソフトウェア設計の実践的な考え方
masuda220
PRO
4
540
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
0
150
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The World Runs on Bad Software
bkeepers
PRO
71
11k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
KATA
mclloyd
32
15k
Typedesign – Prime Four
hannesfritz
42
2.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6.1k
Build your cross-platform service in a week with App Engine
jlugia
232
18k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
850
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Transcript
Dependency Injection はじめの一歩 2019/10/24 .NET Core 3.0 リリース記念 C# Tokyo
2019/10
自己紹介 • ふるかわ @futa_ttjh • 都内IT企業勤務のエンジニア • C#、Xamarin.Formsが好き • Blog
https://takataka430.hatenablog.com/ 2
本発表について Dependency Injectionについて、自分が調べて理解している、と思っている範囲のこと を発表します。 コードよりも図が多め 間違いなどあれば教えてください! 3
Today’s Goal 以下のような方にDependency Injectionについてイメージしてもらう • Dependency Injection 初めて聞いた • Dependency
Injection 聞いたことあるけどよくわからない • 依存性の注入?? 何それ怖い・・・ 4
アプリを作る上で大事なこと 変化に強いことが重要! 機能の追加 顧客からの要望 外部環境の変化 ・・・etc 5
変化に強いとは??? 6
変化に弱い→クラス間で直接の依存関係がある場合 7 HogeHoge Class Contoso Class 直接利用 Contoso Class が必要
public class HogeHoge { Contoso contoso = new Contoso(); ・ ・ ・ コードで書くと
依存している事によるデメリット ・あるクラスが特定の他のクラスに強く結びついている →カスタマイズが難しい ・利用している外部のクラスがないと動かない →単体テストがやりにくい 8
Dependency Injection(DI) とは あるクラスが別のクラスを利用するとき、インスタンスを外部から引き渡す仕組み(直接 new はしない) 9 HogeHoge Contoso Contoso
Class が必要 IContoso Class Class Interface
コードでの例(DIを利用) public class HogeHoge { private IContoso _contoso; public HogeHoge(IContoso
contoso) { _contoso = contoso; } ・ ・ ・ 10 コンストラクタでインスタンスを 外から受け取っている
【依存性の注入】の個人的理解 11 依存性 →外側から持ってくる必要のあるインスタンス(オブジェクト) 注入 →クラスの中で生成するのではなく、引数として受け取る
Before After 12 Without DI With DI App obj2 obj3
obj4 App obj1 obj2 obj3 obj4 obj1 部品は固定 部品をカスタマイズできる
.NET Core におけるDI .NET Coreでは標準でDIをサポート 13 https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0
Blazorでも使われています 14 https://docs.microsoft.com/ja-jp/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.0
まとめ ・クラスの結合が強いと変更に弱くなってしまう →そこでDI! ・DIではオブジェクトを引数として受け取る ・DIのメリット ・カスタマイズがしやすい ・単体テストがしやすい 15
ご清聴ありがとうございました 16