Intro to Dependency Injection & Inversion of Control
-
Upload
kaseem-rios -
Category
Documents
-
view
62 -
download
2
description
Transcript of Intro to Dependency Injection & Inversion of Control
![Page 1: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/1.jpg)
Presenter - Donn Felker
![Page 2: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/2.jpg)
Senior Consultant for Microsoft Gold Certified Partner- Statêra.
8 years of experience in developing and architecting enterprise solutions.
Certifications: ◦ Microsoft Certified Technology Specialist◦ Certified SCRUM Master◦ ITIL Foundation Certified
Bachelors of Science in Software Engineering Member of FooTheory Community
(www.footheory.com) Blog: http://blog.donnfelker.com Email: [email protected]
![Page 3: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/3.jpg)
Present Dependency Injection and Inversion of Control in an understandable fashion
Present each topic at a detailed but comprehendible level
Give you the resources used in this talk so you can reference them in the future.
![Page 4: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/4.jpg)
Confuse you.
Pretend to be an authority in patterns, because, well … I’m not.
Get all “Fowler” on you.
Have theoretical conversations about practicality of the example apps I’m using for demonstration purposes.
![Page 5: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/5.jpg)
What is a Dependency? Dependency Injection Pros/Cons Simple Application Architecture Example Application High Level Architecture Demonstration 1
◦ Identifying and Breaking dependencies What is Inversion of Control Demonstration 2
◦ Custom Dependency Container◦ Introducing Microsoft Unity Container
Questions
![Page 6: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/6.jpg)
Some common dependencies include: ◦ Application Layers
Data Access Layer & Databases Business Layer
◦ External services & Components Web Services Third Party Components
◦ .NET Framework Components File Objects (File.Delete(…), Directory.Exists(…)) Web Objects (HttpContext, Session, Request, etc)
![Page 7: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/7.jpg)
CI Server
BROKEN BUILD!
![Page 8: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/8.jpg)
![Page 9: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/9.jpg)
![Page 10: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/10.jpg)
Code is tightly coupled Difficult to isolate when testing Difficult to maintain
◦ If I change ComponentX how do I know what else it will affect? Did I break anything? If tests are in place they can be your safety net
![Page 11: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/11.jpg)
The ability to supply (inject) an external dependency into a software component.
Types of Dependency Injection: ◦ Constructor (Most popular)◦ Setter ◦ Method
![Page 12: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/12.jpg)
Injecting a ICustomerRepository and a ICustomerDTOMapper through the constructor.
Note: This is the most popular type of injection.
![Page 13: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/13.jpg)
Injecting a ICustomerRepository through the setter.
![Page 14: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/14.jpg)
Injecting a ICustomerRepository as well as an integer dependency.
![Page 15: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/15.jpg)
Pros◦ Loosely Coupled◦ Increases Testability (A LOT!)◦ Separates components cleanly◦ Allows for use of Inversion of Control Container
Cons◦ Increases code complexity◦ Some Jr. Developers find it difficult to understand
at First◦ Can Complicate Debugging at First◦ Complicates following Code Flow
![Page 16: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/16.jpg)
![Page 17: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/17.jpg)
Lets See Some Code…
![Page 18: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/18.jpg)
Sometimes referred to as Dependency Inversion Principle (DIP)◦ The principle states that high level or low level
modules should not depend upon each other, instead they should depend upon abstractions.
Specific implementations (object instances) are deferred to a higher level of abstraction of control.◦ Examples:
Parent class(es) A Container
Referred to as the “Hollywood Principle”◦ “Don’t call us, we will call you.”
![Page 19: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/19.jpg)
The best example is to see it in code.
![Page 20: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/20.jpg)
![Page 21: Intro to Dependency Injection & Inversion of Control](https://reader035.fdocuments.net/reader035/viewer/2022062217/56812ecb550346895d946a6f/html5/thumbnails/21.jpg)
Containers◦ Windsor: http://shrinkster.com/ym6◦ Unity: http://shrinkster.com/ym5◦ Structure Map: http://shrinkster.com/ym7 ◦ Spring.NET: http://shrinkster.com/ym8
Unity Screencast (PnP Guidance)◦ http://shrinkster.com/yma
Good Book to help break dependencies: ◦ Working Effectively with Legacy Code (Michael Feathers)
http://shrinkster.com/ym9
Contact Me◦ [email protected]◦ http://blog.donnfelker.com
All resources from this talk will be posted within 24 hours on this blog.