Dependency Inversion Principle Jon McBee President Composed Systems
Dependency Inversion Principle Depend upon abstract entities, not concrete entities
A. High-level modules should not depend on low-level modules 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 A. High-level modules should not depend on low-level modules. Both should depend on abstractions. Message Broker Message Transport Notifier Queue AF Message
B. Abstractions should not depend on details 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 B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Adding an Event Message Transport type should not Cause the Message Transport interface to change Message Transport Notifier Queue AF Message Event
B. Abstractions should not depend on details B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Message Transport Event
Dependency Inversion Principle
Two Types of Dependency Runtime dependency exists whenever two modules interact at runtime 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 Event Horizon Calculate Mass
Source Code Dependency Tree Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Event Horizon Calculate Mass
Calculate Tidal Forces 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 http://www.labviewcraftsmen.com/blog/fun-with-the-actor-framework-low-coupled-messaging
Dependency Inversion Source Code Dependency Tree Runtime Dependency Tree Calculate Tidal Forces Abstract Calculate Mass Calculate Tidal Forces “Dependencies are inverted whenever the source code dependencies oppose the direction of the flow of control” -Bob Martin + Calculate Mass Calculate Mass Calculate Mass + Calculate Mass + Calculate Mass
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 This would violate SRP! This would violate OCP! 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? Employee iDataWriter + Write XML DB + Write + Write
Dependency Injection Dependency Injection is NOT Dependency Inversion! You must first invert your dependencies to properly apply dependency injection Dependency Injection Dependency Inversion
Demo: DIP in the Wild
Questions?