Dependency Inversion, Dependency Injection and Inversion of Control Dependency Inversion Principle by Robert Martin of Agile Fame
Dependency Inversion Principle History Postulated by Robert C. Martin Described the paper Object Oriented Design Quality Metrics: an analysis of dependencies, an article in the C++ Report in May 1996 entitled The Dependency Inversion Principle In group that began Agile Principles which came out of Extreme Programming Founder Of SOLID: Single responsibility principle, Open/closed principle, Liskov-substitution principle, Interface segregation principle, Dependency inversion principle
IoC Vocabulary
Why Dependency Inversion Needed
Dependency Not Inverted from Previous Slide Example: Low Level Classes Defining Interface
Solution: Dependency Inversion Where High Level Class Defines the Interface for Low Level Classes
From Bob Martin s 1996 Paper on DIP High-level modules should not depend on low-level modules. Both should depend on abstractions Abstractions should not depend on details. Details should depend on abstractions Simple problem presented in paper is a copy program that reads from a keyboard and writes to a printer Become problem when must also write to disk so Copy program now has switch between two interfaces in printer and disk
DI needed to solve problem where high level Copy must switch between 2 different low level interfaces
DIP solves Copy problem with interfaces for Reader and Writer that low level classes must implement
Inversion of Control (IoC) IoC in an implementation of Dependency Inversion Principle Some types of IoC (many definitions out there, however) Interface Inversion: control of interface between 2 components or systems Flow Inversion: control of the flow between an application s layers Creation Inversion: control of dependency creation and binding of classes
How does IoC relate to DIP? Low level module or layer should not control high level module or layer IoC is a way to implement Dependency Inversion Principle
DIP and IoC General Implementation Types
Interface Inversion (the Provider Model)
Flow Inversion
Flow Inversion: App not in control, flow now inverted so that user is in control of application
Creation Inversion Normally objects are created in the class that s using it. MyClass myobject = new MyClass(); If the creation is done in a high level class to instantiate the lower level class, they are still dependent upon each other even if MyClass is an interface In Creation Inversion, create the object outside the class they are used in Creation Inversion uses the GoF Factory Pattern to eliminate switch statements that usually necessitate cutting-and-pasting of same code everywhere
Factory Pattern for Creation Inversion Define interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Moves creation of an low-level object to a factory class outside the highlevel class: MyClass myobject = MyFactory.CreateNewObject(); Not the only way for Creation Inversion since any time moving object outside the class using it, it s creation inversion
Many Creation Inversion Types
Summary So Far
Dependency Injection (DI) Overview What is Dependency Injection? Constructor Injection (Most Common) Setter Injection (Less Common) Interface Injection (Least Common) DI Caution
What is Dependency Injection? Dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. DI is moving creation and binding of dependency outside of class that depends on it Simple example: packing lunch versus having lunch provided by Service.
Decoupling Dependency doesn t remove Dependency since MyClass creates interface
Class Outside MyClass acts as Injector to remove Dependency (Dependency Injection)
Constructor Injection via Outside Class Simple: Pass dependency to dependent class via constructor
Setter Injection: Create property setter in dependent class as injector method but order matters
Interface Injection: Dependent class implements interface and injector uses interface to set dependency, not common
IoC Container
IoC Container Visualization
IoC Container (From Previous Example)
ASP.NET 5 IoC or DI Container The core features DI container abstracted to the IServiceProvider interface The DI container supports just four modes of operation: Instance: A specific instance is given all the time. You are responsible for its initial creation. Transient: A new instance is created every time. Singleton: A single instance is created and acts like a singleton. Scoped: A single instance is created inside the current scope. It is equivalent to Singleton in the current scope.
IoC, DI Container Demo We don't need to know anything about the service,other than that it implements the IProjectService interface, an interface custom to our demo project. IProjectService has one method, GetOrganization(). This method retrieves an organization and its corresponding list of projects. Demo
Summary Covered Dependency Inversion Principle Covered Inversion of Control Covered Dependency Injection Gave a demo using IoC