$30 off During Our Annual Pro Sale. View Details »
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
850
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
1.1k
BlazorのPWAを使ってみた / Tried using PWA
takahiro901
0
1.4k
C#勉強法: コンソールでアプリを作ろう / 20190910 C# Tokyo
takahiro901
0
790
Xamarin.Formsで自分用の家計簿アプリを作ってみた / 20190831 JXUG
takahiro901
0
940
プログラミング初心者が Xamarin.Formsやってみた / 20180523 JXUG
takahiro901
0
440
Other Decks in Programming
See All in Programming
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
230
ハイパーメディア駆動アプリケーションとIslandアーキテクチャ: htmxによるWebアプリケーション開発と動的UIの局所的適用
nowaki28
0
380
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
160
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
120
Developing static sites with Ruby
okuramasafumi
0
240
CSC509 Lecture 14
javiergs
PRO
0
220
How Software Deployment tools have changed in the past 20 years
geshan
0
28k
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
5
1.9k
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
140
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
AIコーディングエージェント(Gemini)
kondai24
0
190
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
120
Featured
See All Featured
BBQ
matthewcrist
89
9.9k
[RailsConf 2023] Rails as a piece of cake
palkan
58
6.1k
Unsuck your backbone
ammeep
671
58k
KATA
mclloyd
PRO
32
15k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Building Adaptive Systems
keathley
44
2.9k
Embracing the Ebb and Flow
colly
88
4.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.1k
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