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
880
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dependency Injection はじめの一歩 / 20191024 C#Tokyo
2019/10/24
.NET Core 3.0 リリース記念 C# Tokyo 2019/10 発表資料
Taka
October 24, 2019
More Decks by Taka
See All by Taka
フロントエンド初心者が Blazorを使ってみた / 20220428 C#Tokyo
takahiro901
0
500
HoloLens 2なしでも MRを楽しむ!
takahiro901
0
1.2k
BlazorのPWAを使ってみた / Tried using PWA
takahiro901
0
1.5k
C#勉強法: コンソールでアプリを作ろう / 20190910 C# Tokyo
takahiro901
0
860
Xamarin.Formsで自分用の家計簿アプリを作ってみた / 20190831 JXUG
takahiro901
0
990
プログラミング初心者が Xamarin.Formsやってみた / 20180523 JXUG
takahiro901
0
450
Other Decks in Programming
See All in Programming
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
230
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
260
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
250
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
140
Go 1.27からのGODEBUG / Go 1.27 リリースパーティ #go127party
mazrean
0
290
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
140
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
720
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
160
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
100
Featured
See All Featured
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
230
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
490
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
810
Designing for Performance
lara
611
70k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
A Tale of Four Properties
chriscoyier
163
24k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
320
Making the Leap to Tech Lead
cromwellryan
135
10k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Paper Plane
katiecoart
PRO
2
53k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
430
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
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