Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT.

download Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT.

If you can't read please download the document

description

A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.

Transcript of Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT.

Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT Dependency Inversion Principle Depend upon abstract entities, not concrete entities A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions. A. High-level modules should not depend on low-level modules. Both should depend on abstractions. Message Broker Message Transport QueueNotifierAF Message B. Abstractions should not depend on details. Details should depend on abstractions. By passing dependencies to classes as abstractions, you remove the need to program dependency specific By giving a craftsman a Craftsman Bolt On base, you remove the need for the craftsman to lug around a bag of tools B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Message Transport QueueNotifierAF MessageEvent Adding an Event Message Transport type should not Cause the Message Transport interface to change B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Message Transport Event Dependency Inversion Principle Two Types of Dependency 1.Runtime dependency exists whenever two modules interact at runtime 2.Source code dependency exists when a method defined by one module is called by another module Runtime Dependency Tree Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Mass Calculate Event Horizon Source Code Dependency Tree Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Mass Calculate Event Horizon Break the Source Code Dependency Calculate Tidal Forces Calculate Mass + Calculate Mass Break the Source Code Dependency Calculate Tidal Forces Abstract Calculate Mass + Calculate Mass Calculate Mass + Calculate Mass Dependency Inversion Calculate Tidal Forces Abstract Calculate Mass + Calculate Mass Calculate Mass + Calculate Mass Calculate Tidal Forces Calculate Mass + Calculate Mass Source Code Dependency TreeRuntime Dependency Tree Dependencies are inverted whenever the source code dependencies oppose the direction of the flow of control -Bob Martin Discussion Question Q: Assume that we are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to both XML and to a database. How should we give the Employee class this ability? P1: Should we give the Employee class ToXML.vi and ToDB.vi methods? P2: Should we give the Employee class a Write.vi method that takes as an input a flag for either writing to XML or to DB using an XML or DB object respectively? Discussion Question Q: Assume that we have are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to XML and to a database. How should we give the Employee class this ability? P1: Should we give the Employee class ToXML.vi and ToDB.vi methods? This would violate SRP! P2: Should we give the Employee class a Write.vi method that takes as an input a flag for either writing to XML or to DB using an XML or DB object respectively? This would violate OCP! Discussion Question Q: Assume that we have are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to XML and to a database. How should we give the Employee class this ability? EmployeeiDataWriter + Write XML + Write DB + Write Dependency Injection Dependency Injection is NOT Dependency Inversion! You must first invert your dependencies to properly apply dependency injection Dependency Inversion Dependency Injection Demo: DIP in the Wild Questions?